Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> */ private RealMatrix weightMatrixSqrt; /** Cost value (square root of the sum of the residuals). */ private double cost; /** * @param checker Convergence checker. */ protected AbstractLeastSquaresOptimizer(ConvergenceChecker<PointVectorValuePair> checker) { super(checker); } /** * Computes the weighted Jacobian matrix. * * @param params Model parameters at which to compute the Jacobian. * @return the weighted Jacobian: W<sup>1/2</sup> J. * @throws DimensionMismatchException if the Jacobian dimension does not * match problem dimension. */ protected RealMatrix computeWeightedJacobian(double[] params) { return weightMatrixSqrt.multiply(MatrixUtils.createRealMatrix(computeJacobian(params))); } /** * Computes the cost. * * @param residuals Residuals. * @return the cost. * @see #computeResiduals(double[]) */ protected double computeCost(double[] residuals) { final ArrayRealVector r = new ArrayRealVector(residuals); return FastMath.sqrt(r.dotProduct(getWeight().operate(r))); } /** * Gets the root-mean-square (RMS) value. * * The RMS the root of the arithmetic mean of the square of all weighted * residuals. * This is related to the criterion that is minimized by the optimizer * as follows: If <em>c</em> if the criterion, and <em>n</em> is the * number of measurements, then the RMS is <em>sqrt (c/n)</em>. * * @return the RMS value. */ public double getRMS() { return FastMath.sqrt(getChiSquare() / getTargetSize()); } /** * Get a Chi-Square-like value assuming the N residuals follow N * distinct normal distributions centered on 0 and whose variances are * the reciprocal of the weights. * @return chi-square value */ public double getChiSquare() { return cost * cost; } /** * Gets the square-root of the weight matrix. * * @return the square-root of the weight matrix. */ public RealMatrix getWeightSquareRoot() { return weightMatrixSqrt.copy(); } /**

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>params, covarianceSingularityThreshold); for (int i = 0; i < nC; ++i) { sig[i] = FastMath.sqrt(cov[i][i]); } return sig; } /** * {@inheritDoc} * * @param optData Optimization data. The following data will be looked for: * <ul> * <li>{@link org.apache.commons.math3.optim.MaxEval}</li> * <li>{@link org.apache.commons.math3.optim.InitialGuess}</li> * <li>{@link org.apache.commons.math3.optim.SimpleBounds}</li> * <li>{@link org.apache.commons.math3.optim.nonlinear.vector.Target}</li> * <li>{@link org.apache.commons.math3.optim.nonlinear.vector.Weight}</li> * <li>{@link org.apache.commons.math3.optim.nonlinear.vector.ModelFunction}</li> * <li>{@link org.apache.commons.math3.optim.nonlinear.vector.ModelFunctionJacobian}</li> * </ul> * @return {@inheritDoc} * @throws TooManyEvaluationsException if the maximal number of * evaluations is exceeded. * @throws DimensionMismatchException if the initial guess, target, and weight * arguments have inconsistent dimensions. */ @Override public PointVectorValuePair optimize(OptimizationData... optData) throws TooManyEvaluationsException { // Retrieve settings. parseOptimizationData(optData); // Set up base class and perform computation. return super.optimize(optData); } /** * Computes the residuals. * The residual is the difference between the observed (target) * values and the model (objective function) value. * There is one residual for each element of the vector-valued * function. * * @param objectiveValue Value of the the objective function. This is * the value returned from a call to * {@link #computeObjectiveValue(double[]) computeObjectiveValue} * (whose array argument contains the model parameters). * @return the residuals. * @throws DimensionMismatchException if {@code params} has a wrong * length. */ protected double[] computeResiduals(double[] objectiveValue) { final double[] target =

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.math3.util; import java.util.Arrays; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NotFiniteNumberException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.util.Localizable; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Miscellaneous utility functions. * * @see ArithmeticUtils * @see Precision * @see MathArrays * * @version $Id$ */ public final class MathUtils { /** * 2 &pi;. * @since 2.1 */ public static final double TWO_PI = 2 * FastMath.PI; /** * Class contains only static methods. */ private MathUtils() {} /** * Returns an integer hash code representing the given double value. * * @param value the value to be hashed * @return the hash code */ public static int hash(double value) { return new Double(value).hashCode(); } /** * Returns an integer hash code representing the given double array. * * @param value the value to be hashed (may be null) * @return the hash code * @since 1.2 */ public static int hash(double[] value) { return Arrays.hashCode(value); } /** * Normalize an angle in a 2&pi wide interval around a center value.

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> * <p>This method has three main uses:</p> * <ul> * <li>normalize an angle between 0 and 2&pi;:<br/> * {@code a = MathUtils.normalizeAngle(a, FastMath.PI);}</li> * <li>normalize an angle between -&pi; and +&pi;<br/> * {@code a = MathUtils.normalizeAngle(a, 0.0);}</li> * <li>compute the angle between two defining angular positions:<br> * {@code angle = MathUtils.normalizeAngle(end, start) - start;}</li> * </ul> * <p>Note that due to numerical accuracy and since &pi; cannot be represented * exactly, the result interval is <em>closed</em>, it cannot be half-closed * as would be more satisfactory in a purely mathematical view.</p> * @param a angle to normalize * @param center center of the desired 2&pi; interval for the result * @return a-2k&pi; with integer k and center-&pi; &lt;= a-2k&pi; &lt;= center+&pi; * @since 1.2 */ public static double normalizeAngle(double a, double center) { return a - TWO_PI * FastMath.floor((a + FastMath.PI - center) / TWO_PI); } /** * <p>Reduce {@code |a - offset|} to the primary interval * {@code [0, |period|)}.</p> * * <p>Specifically, the value returned is <br/> * {@code a - |period| * floor((a - offset) / |period|) - offset}.</p> * * <p>If any of the parameters are {@code NaN} or infinite, the result is * {@code NaN}.</p> * * @param a Value to reduce. * @param period Period. * @param offset Value that will be mapped to {@code 0}. * @return the value, within the interval {@code [0 |period|)}, * that corresponds to {@code a}. */ public static double reduce(double a, double period, double offset) { final double p = FastMath.abs(period); return a - p * FastMath.floor

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>(separator); } CompositeFormat.formatDouble(vector.getEntry(i), format, toAppendTo, pos); } // format suffix toAppendTo.append(suffix); return toAppendTo; } /** * Parse a string to produce a {@link RealVector} object. * * @param source String to parse. * @return the parsed {@link RealVector} object. * @throws MathParseException if the beginning of the specified string * cannot be parsed. */ public ArrayRealVector parse(String source) { final ParsePosition parsePosition = new ParsePosition(0); final ArrayRealVector result = parse(source, parsePosition); if (parsePosition.getIndex() == 0) { throw new MathParseException(source, parsePosition.getErrorIndex(), ArrayRealVector.class); } return result; } /** * Parse a string to produce a {@link RealVector} object. * * @param source String to parse. * @param pos input/ouput parsing parameter. * @return the parsed {@link RealVector} object. */ public ArrayRealVector parse(String source, ParsePosition pos) { int initialIndex = pos.getIndex(); // parse prefix CompositeFormat.parseAndIgnoreWhitespace(source, pos); if (!CompositeFormat.parseFixedstring(source, trimmedPrefix, pos)) { return null; } // parse components List<Number> components = new ArrayList<Number>(); for (boolean loop = true; loop;){ if (!components.isEmpty()) { CompositeFormat.parseAndIgnoreWhitespace(source, pos); if (!CompositeFormat.parseFixedstring(source, trimmedSeparator, pos)) { loop = false; } } if (loop) { CompositeFormat.parseAndIgnoreWhitespace(source, pos); Number component = CompositeFormat.parseNumber(source, format, pos); if (component != null) { components.add(component); } else { // invalid component // set index back to initial, error index should already be set pos.setIndex(initialIndex); return null; } } } // parse suffix CompositeFormat.parseAndIgnoreWhitespace(source, pos); if (!CompositeFormat.parseFixedstring(source, trimmedSuffix, pos)) { return null; } // build vector double[] data = new double[components.size()];

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> = m.entries.iterator(); iterator.hasNext();) { iterator.advance(); final int row = iterator.key() / columns; final int col = iterator.key() - row * columns; out.setEntry(row, col, getEntry(row, col) + iterator.value()); } return out; } /** {@inheritDoc} */ @Override public OpenMapRealMatrix subtract(final RealMatrix m) throws MatrixDimensionMismatchException { try { return subtract((OpenMapRealMatrix) m); } catch (ClassCastException cce) { return (OpenMapRealMatrix) super.subtract(m); } } /** * Subtract {@code m} from this matrix. * * @param m Matrix to be subtracted. * @return {@code this} - {@code m}. * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this}. */ public OpenMapRealMatrix subtract(OpenMapRealMatrix m) throws MatrixDimensionMismatchException { MatrixUtils.checkAdditionCompatible(this, m); final OpenMapRealMatrix out = new OpenMapRealMatrix(this); for (OpenIntToDoubleHashMap.Iterator iterator = m.entries.iterator(); iterator.hasNext();) { iterator.advance(); final int row = iterator.key() / columns; final int col = iterator.key() - row * columns; out.setEntry(row, col, getEntry(row, col) - iterator.value()); } return out; } /** * {@inheritDoc} * * @throws NumberIsTooLargeException if {@code m} is an * {@code OpenMapRealMatrix}, and the total number of entries of the product * is larger than {@code Integer.MAX_VALUE}. */ @Override public RealMatrix multiply(final RealMatrix m) throws DimensionMismatchException, NumberIsTooLargeException { try { return multiply((OpenMapRealMatrix) m); } catch (ClassCastException cce) { MatrixUtils.checkMultiplicationCompatible(this, m); final int outCols = m.getColumnDimension(); final BlockRealMatrix out = new BlockRealMatrix(rows, outCols); for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();) { iterator.advance(); final double value = iterator.value(); final int key = iterator

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>.key(); final int i = key / columns; final int k = key % columns; for (int j = 0; j < outCols; ++j) { out.addToEntry(i, j, value * m.getEntry(k, j)); } } return out; } } /** * Postmultiply this matrix by {@code m}. * * @param m Matrix to postmultiply by. * @return {@code this} * {@code m}. * @throws DimensionMismatchException if the number of rows of {@code m} * differ from the number of columns of {@code this} matrix. * @throws NumberIsTooLargeException if the total number of entries of the * product is larger than {@code Integer.MAX_VALUE}. */ public OpenMapRealMatrix multiply(OpenMapRealMatrix m) throws DimensionMismatchException, NumberIsTooLargeException { // Safety check. MatrixUtils.checkMultiplicationCompatible(this, m); final int outCols = m.getColumnDimension(); OpenMapRealMatrix out = new OpenMapRealMatrix(rows, outCols); for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();) { iterator.advance(); final double value = iterator.value(); final int key = iterator.key(); final int i = key / columns; final int k = key % columns; for (int j = 0; j < outCols; ++j) { final int rightKey = m.computeKey(k, j); if (m.entries.containsKey(rightKey)) { final int outKey = out.computeKey(i, j); final double outValue = out.entries.get(outKey) + value * m.entries.get(rightKey); if (outValue == 0.0) { out.entries.remove(outKey); } else { out.entries.put(outKey, outValue); } } } } return out; } /** {@inheritDoc} */ @Override public double getEntry(int row, int column) throws OutOfRangeException { MatrixUtils.checkRowIndex(this, row); MatrixUtils.checkColumnIndex(this, column); return entries.get(computeKey(row, column)); } /** {@inheritDoc} */ @Override public int

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> * * @return a fresh copy of the coefficients array. */ public double[] getCoefficients() { return coefficients.clone(); } /** * Uses Horner's Method to evaluate the polynomial with the given coefficients at * the argument. * * @param coefficients Coefficients of the polynomial to evaluate. * @param argument Input value. * @return the value of the polynomial. * @throws NoDataException if {@code coefficients} is empty. * @throws NullArgumentException if {@code coefficients} is {@code null}. */ protected static double evaluate(double[] coefficients, double argument) throws NullArgumentException, NoDataException { MathUtils.checkNotNull(coefficients); int n = coefficients.length; if (n == 0) { throw new NoDataException(LocalizedFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY); } double result = coefficients[n - 1]; for (int j = n - 2; j >= 0; j--) { result = argument * result + coefficients[j]; } return result; } /** {@inheritDoc} * @since 3.1 * @throws NoDataException if {@code coefficients} is empty. * @throws NullArgumentException if {@code coefficients} is {@code null}. */ public DerivativeStructure value(final DerivativeStructure t) throws NullArgumentException, NoDataException { MathUtils.checkNotNull(coefficients); int n = coefficients.length; if (n == 0) { throw new NoDataException(LocalizedFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY); } DerivativeStructure result = new DerivativeStructure(t.getFreeParameters(), t.getOrder(), coefficients[n - 1]); for (int j = n - 2; j >= 0; j--) { result = result.multiply(t).add(coefficients[j]); } return result; } /** * Add a polynomial to the instance. * * @param p Polynomial to add. * @return a new polynomial which is the sum of the instance and {@code p}. */ public PolynomialFunction add(final PolynomialFunction p) { // identify the lowest degree polynomial final int lowLength = FastMath.min(coefficients.length, p.coefficients.length); final int highLength = Fast

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>Math.max(coefficients.length, p.coefficients.length); // build the coefficients array double[] newCoefficients = new double[highLength]; for (int i = 0; i < lowLength; ++i) { newCoefficients[i] = coefficients[i] + p.coefficients[i]; } System.arraycopy((coefficients.length < p.coefficients.length) ? p.coefficients : coefficients, lowLength, newCoefficients, lowLength, highLength - lowLength); return new PolynomialFunction(newCoefficients); } /** * Subtract a polynomial from the instance. * * @param p Polynomial to subtract. * @return a new polynomial which is the difference the instance minus {@code p}. */ public PolynomialFunction subtract(final PolynomialFunction p) { // identify the lowest degree polynomial int lowLength = FastMath.min(coefficients.length, p.coefficients.length); int highLength = FastMath.max(coefficients.length, p.coefficients.length); // build the coefficients array double[] newCoefficients = new double[highLength]; for (int i = 0; i < lowLength; ++i) { newCoefficients[i] = coefficients[i] - p.coefficients[i]; } if (coefficients.length < p.coefficients.length) { for (int i = lowLength; i < highLength; ++i) { newCoefficients[i] = -p.coefficients[i]; } } else { System.arraycopy(coefficients, lowLength, newCoefficients, lowLength, highLength - lowLength); } return new PolynomialFunction(newCoefficients); } /** * Negate the instance. * * @return a new polynomial. */ public PolynomialFunction negate() { double[] newCoefficients = new double[coefficients.length]; for (int i = 0; i < coefficients.length; ++i) { newCoefficients[i] = -coefficients[i]; } return new PolynomialFunction(newCoefficients); } /** * Multiply the instance by a polynomial. * * @param p Polynomial to multiply by. * @return a new polynomial. */ public PolynomialFunction multiply(final PolynomialFunction

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> p) { double[] newCoefficients = new double[coefficients.length + p.coefficients.length - 1]; for (int i = 0; i < newCoefficients.length; ++i) { newCoefficients[i] = 0.0; for (int j = FastMath.max(0, i + 1 - p.coefficients.length); j < FastMath.min(coefficients.length, i + 1); ++j) { newCoefficients[i] += coefficients[j] * p.coefficients[i-j]; } } return new PolynomialFunction(newCoefficients); } /** * Returns the coefficients of the derivative of the polynomial with the given coefficients. * * @param coefficients Coefficients of the polynomial to differentiate. * @return the coefficients of the derivative or {@code null} if coefficients has length 1. * @throws NoDataException if {@code coefficients} is empty. * @throws NullArgumentException if {@code coefficients} is {@code null}. */ protected static double[] differentiate(double[] coefficients) throws NullArgumentException, NoDataException { MathUtils.checkNotNull(coefficients); int n = coefficients.length; if (n == 0) { throw new NoDataException(LocalizedFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY); } if (n == 1) { return new double[]{0}; } double[] result = new double[n - 1]; for (int i = n - 1; i > 0; i--) { result[i - 1] = i * coefficients[i]; } return result; } /** * Returns the derivative as a {@link PolynomialFunction}. * * @return the derivative polynomial. */ public PolynomialFunction polynomialDerivative() { return new PolynomialFunction(differentiate(coefficients)); } /** * Returns the derivative as a {@link UnivariateFunction}. * * @return the derivative function. */ public UnivariateFunction derivative() { return polynomialDerivative(); } /** * Returns a string representation of the polynomial. * * <p>The representation is user oriented. Terms are displayed lowest * degrees first. The multiplications signs, coefficients equals to * one and null terms are not displayed (except if the polynomial

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> is 0, * in which case the 0 constant term is displayed). Addition of terms * with negative coefficients are replaced by subtraction of terms * with positive coefficients except for the first displayed term * (i.e. we display <code>-3</code> for a constant negative polynomial, * but <code>1 - 3 x + x^2</code> if the negative coefficient is not * the first one displayed).</p> * * @return a string representation of the polynomial. */ @Override public String toString() { StringBuilder s = new StringBuilder(); if (coefficients[0] == 0.0) { if (coefficients.length == 1) { return "0"; } } else { s.append(toString(coefficients[0])); } for (int i = 1; i < coefficients.length; ++i) { if (coefficients[i] != 0) { if (s.length() > 0) { if (coefficients[i] < 0) { s.append(" - "); } else { s.append(" + "); } } else { if (coefficients[i] < 0) { s.append("-"); } } double absAi = FastMath.abs(coefficients[i]); if ((absAi - 1) != 0) { s.append(toString(absAi)); s.append(' '); } s.append("x"); if (i > 1) { s.append('^'); s.append(Integer.toString(i)); } } } return s.toString(); } /** * Creates a string representing a coefficient, removing ".0" endings. * * @param coeff Coefficient. * @return a string representation of {@code coeff}. */ private static String toString(double coeff) { final String c = Double.toString(coeff); if (c.endsWith(".0")) { return c.substring(0, c.length() - 2); } else { return c; } } /** {@inheritDoc} */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + Arrays.hashCode(coefficients); return result; } /**

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> */ protected AbstractRealMatrix() {} /** * Create a new RealMatrix with the supplied row and column dimensions. * * @param rowDimension the number of rows in the new matrix * @param columnDimension the number of columns in the new matrix * @throws NotStrictlyPositiveException if row or column dimension is not positive */ protected AbstractRealMatrix(final int rowDimension, final int columnDimension) throws NotStrictlyPositiveException { if (rowDimension < 1) { throw new NotStrictlyPositiveException(rowDimension); } if (columnDimension < 1) { throw new NotStrictlyPositiveException(columnDimension); } } /** {@inheritDoc} */ public RealMatrix add(RealMatrix m) throws MatrixDimensionMismatchException { MatrixUtils.checkAdditionCompatible(this, m); final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final RealMatrix out = createMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < columnCount; ++col) { out.setEntry(row, col, getEntry(row, col) + m.getEntry(row, col)); } } return out; } /** {@inheritDoc} */ public RealMatrix subtract(final RealMatrix m) throws MatrixDimensionMismatchException { MatrixUtils.checkSubtractionCompatible(this, m); final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final RealMatrix out = createMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < columnCount; ++col) { out.setEntry(row, col, getEntry(row, col) - m.getEntry(row, col)); } } return out; } /** {@inheritDoc} */ public RealMatrix scalarAdd(final double d) { final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final RealMatrix out = createMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < columnCount; ++col) { out.setEntry(row, col

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>, getEntry(row, col) + d); } } return out; } /** {@inheritDoc} */ public RealMatrix scalarMultiply(final double d) { final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final RealMatrix out = createMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < columnCount; ++col) { out.setEntry(row, col, getEntry(row, col) * d); } } return out; } /** {@inheritDoc} */ public RealMatrix multiply(final RealMatrix m) throws DimensionMismatchException { MatrixUtils.checkMultiplicationCompatible(this, m); final int nRows = getRowDimension(); final int nCols = m.getColumnDimension(); final int nSum = getColumnDimension(); final RealMatrix out = createMatrix(nRows, nCols); for (int row = 0; row < nRows; ++row) { for (int col = 0; col < nCols; ++col) { double sum = 0; for (int i = 0; i < nSum; ++i) { sum += getEntry(row, i) * m.getEntry(i, col); } out.setEntry(row, col, sum); } } return out; } /** {@inheritDoc} */ public RealMatrix preMultiply(final RealMatrix m) throws DimensionMismatchException { return m.multiply(this); } /** {@inheritDoc} */ public RealMatrix power(final int p) throws NotPositiveException, NonSquareMatrixException { if (p < 0) { throw new NotPositiveException(LocalizedFormats.NOT_POSITIVE_EXPONENT, p); } if (!isSquare()) { throw new NonSquareMatrixException(getRowDimension(), getColumnDimension()); } if (p == 0) { return MatrixUtils.createRealIdentityMatrix(this.getRowDimension()); } if (p == 1) { return this.copy(); } final int power = p - 1; /* * Only log_2(p) operations is used by doing as follows: * 5^214 = 5^128 * 5^64

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> * 5^16 * 5^4 * 5^2 * * In general, the same approach is used for A^p. */ final char[] binaryRepresentation = Integer.toBinaryString(power).toCharArray(); final ArrayList<Integer> nonZeroPositions = new ArrayList<Integer>(); int maxI = -1; for (int i = 0; i < binaryRepresentation.length; ++i) { if (binaryRepresentation[i] == '1') { final int pos = binaryRepresentation.length - i - 1; nonZeroPositions.add(pos); // The positions are taken in turn, so maxI is only changed once if (maxI == -1) { maxI = pos; } } } RealMatrix[] results = new RealMatrix[maxI + 1]; results[0] = this.copy(); for (int i = 1; i <= maxI; ++i) { results[i] = results[i-1].multiply(results[i-1]); } RealMatrix result = this.copy(); for (Integer i : nonZeroPositions) { result = result.multiply(results[i]); } return result; } /** {@inheritDoc} */ public double[][] getData() { final double[][] data = new double[getRowDimension()][getColumnDimension()]; for (int i = 0; i < data.length; ++i) { final double[] dataI = data[i]; for (int j = 0; j < dataI.length; ++j) { dataI[j] = getEntry(i, j); } } return data; } /** {@inheritDoc} */ public double getNorm() { return walkInColumnOrder(new RealMatrixPreservingVisitor() { /** Last row index. */ private double endRow; /** Sum of absolute values on one column. */ private double columnSum; /** Maximal sum across all columns. */ private double maxColSum; /** {@inheritDoc} */ public void start(final int rows, final int columns, final int startRow, final int endRow, final int startColumn, final int endColumn) { this.endRow = endRow; columnSum = 0; maxColSum = 0; } /** {@inheritDoc} */ public void visit(final

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> int row, final int column, final double value) { columnSum += FastMath.abs(value); if (row == endRow) { maxColSum = FastMath.max(maxColSum, columnSum); columnSum = 0; } } /** {@inheritDoc} */ public double end() { return maxColSum; } }); } /** {@inheritDoc} */ public double getFrobeniusNorm() { return walkInOptimizedOrder(new RealMatrixPreservingVisitor() { /** Sum of squared entries. */ private double sum; /** {@inheritDoc} */ public void start(final int rows, final int columns, final int startRow, final int endRow, final int startColumn, final int endColumn) { sum = 0; } /** {@inheritDoc} */ public void visit(final int row, final int column, final double value) { sum += value * value; } /** {@inheritDoc} */ public double end() { return FastMath.sqrt(sum); } }); } /** {@inheritDoc} */ public RealMatrix getSubMatrix(final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); final RealMatrix subMatrix = createMatrix(endRow - startRow + 1, endColumn - startColumn + 1); for (int i = startRow; i <= endRow; ++i) { for (int j = startColumn; j <= endColumn; ++j) { subMatrix.setEntry(i - startRow, j - startColumn, getEntry(i, j)); } } return subMatrix; } /** {@inheritDoc} */ public RealMatrix getSubMatrix(final int[] selectedRows, final int[] selectedColumns) throws NullArgumentException, NoDataException, OutOfRangeException { MatrixUtils.checkSubMatrixIndex(this, selectedRows, selectedColumns); final RealMatrix subMatrix = createMatrix(selectedRows.length, selectedColumns.length); subMatrix.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() { /** {@inheritDoc} */ @Override public double visit(final int row, final int column, final double value) {

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> return getEntry(selectedRows[row], selectedColumns[column]); } }); return subMatrix; } /** {@inheritDoc} */ public void copySubMatrix(final int startRow, final int endRow, final int startColumn, final int endColumn, final double[][] destination) throws OutOfRangeException, NumberIsTooSmallException, MatrixDimensionMismatchException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); final int rowsCount = endRow + 1 - startRow; final int columnsCount = endColumn + 1 - startColumn; if ((destination.length < rowsCount) || (destination[0].length < columnsCount)) { throw new MatrixDimensionMismatchException(destination.length, destination[0].length, rowsCount, columnsCount); } walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() { /** Initial row index. */ private int startRow; /** Initial column index. */ private int startColumn; /** {@inheritDoc} */ @Override public void start(final int rows, final int columns, final int startRow, final int endRow, final int startColumn, final int endColumn) { this.startRow = startRow; this.startColumn = startColumn; } /** {@inheritDoc} */ @Override public void visit(final int row, final int column, final double value) { destination[row - startRow][column - startColumn] = value; } }, startRow, endRow, startColumn, endColumn); } /** {@inheritDoc} */ public void copySubMatrix(int[] selectedRows, int[] selectedColumns, double[][] destination) throws OutOfRangeException, NullArgumentException, NoDataException, MatrixDimensionMismatchException { MatrixUtils.checkSubMatrixIndex(this, selectedRows, selectedColumns); if ((destination.length < selectedRows.length) || (destination[0].length < selectedColumns.length)) { throw new MatrixDimensionMismatchException(destination.length, destination[0].length, selectedRows.length, selectedColumns.length); } for (int i = 0; i < selectedRows.length; i++) { final double[] destinationI = destination[i]; for (int j = 0; j < selectedColumns.length; j++) { destinationI[j] = get

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>Entry(selectedRows[i], selectedColumns[j]); } } } /** {@inheritDoc} */ public void setSubMatrix(final double[][] subMatrix, final int row, final int column) throws NoDataException, OutOfRangeException, DimensionMismatchException, NullArgumentException { MathUtils.checkNotNull(subMatrix); final int nRows = subMatrix.length; if (nRows == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); } final int nCols = subMatrix[0].length; if (nCols == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN); } for (int r = 1; r < nRows; ++r) { if (subMatrix[r].length != nCols) { throw new DimensionMismatchException(nCols, subMatrix[r].length); } } MatrixUtils.checkRowIndex(this, row); MatrixUtils.checkColumnIndex(this, column); MatrixUtils.checkRowIndex(this, nRows + row - 1); MatrixUtils.checkColumnIndex(this, nCols + column - 1); for (int i = 0; i < nRows; ++i) { for (int j = 0; j < nCols; ++j) { setEntry(row + i, column + j, subMatrix[i][j]); } } } /** {@inheritDoc} */ public RealMatrix getRowMatrix(final int row) throws OutOfRangeException { MatrixUtils.checkRowIndex(this, row); final int nCols = getColumnDimension(); final RealMatrix out = createMatrix(1, nCols); for (int i = 0; i < nCols; ++i) { out.setEntry(0, i, getEntry(row, i)); } return out; } /** {@inheritDoc} */ public void setRowMatrix(final int row, final RealMatrix matrix) throws OutOfRangeException, MatrixDimensionMismatchException { MatrixUtils.checkRowIndex(this, row); final int nCols = getColumnDimension(); if ((matrix.getRowDimension() != 1) || (matrix.getColumnDimension() != nCols)) { throw new MatrixDimensionMismatchException(matrix.getRowDimension(), matrix.getColumnDimension(), 1, nCols);

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> } for (int i = 0; i < nCols; ++i) { setEntry(row, i, matrix.getEntry(0, i)); } } /** {@inheritDoc} */ public RealMatrix getColumnMatrix(final int column) throws OutOfRangeException { MatrixUtils.checkColumnIndex(this, column); final int nRows = getRowDimension(); final RealMatrix out = createMatrix(nRows, 1); for (int i = 0; i < nRows; ++i) { out.setEntry(i, 0, getEntry(i, column)); } return out; } /** {@inheritDoc} */ public void setColumnMatrix(final int column, final RealMatrix matrix) throws OutOfRangeException, MatrixDimensionMismatchException { MatrixUtils.checkColumnIndex(this, column); final int nRows = getRowDimension(); if ((matrix.getRowDimension() != nRows) || (matrix.getColumnDimension() != 1)) { throw new MatrixDimensionMismatchException(matrix.getRowDimension(), matrix.getColumnDimension(), nRows, 1); } for (int i = 0; i < nRows; ++i) { setEntry(i, column, matrix.getEntry(i, 0)); } } /** {@inheritDoc} */ public RealVector getRowVector(final int row) throws OutOfRangeException { return new ArrayRealVector(getRow(row), false); } /** {@inheritDoc} */ public void setRowVector(final int row, final RealVector vector) throws OutOfRangeException, MatrixDimensionMismatchException { MatrixUtils.checkRowIndex(this, row); final int nCols = getColumnDimension(); if (vector.getDimension() != nCols) { throw new MatrixDimensionMismatchException(1, vector.getDimension(), 1, nCols); } for (int i = 0; i < nCols; ++i) { setEntry(row, i, vector.getEntry(i)); } } /** {@inheritDoc} */ public RealVector getColumnVector(final int column) throws OutOfRangeException { return new ArrayRealVector(getColumn(column), false); } /** {@inheritDoc} */ public void setColumnVector(final int column, final RealVector vector) throws OutOfRangeException, MatrixDimensionMismatchException {

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> MatrixUtils.checkColumnIndex(this, column); final int nRows = getRowDimension(); if (vector.getDimension() != nRows) { throw new MatrixDimensionMismatchException(vector.getDimension(), 1, nRows, 1); } for (int i = 0; i < nRows; ++i) { setEntry(i, column, vector.getEntry(i)); } } /** {@inheritDoc} */ public double[] getRow(final int row) throws OutOfRangeException { MatrixUtils.checkRowIndex(this, row); final int nCols = getColumnDimension(); final double[] out = new double[nCols]; for (int i = 0; i < nCols; ++i) { out[i] = getEntry(row, i); } return out; } /** {@inheritDoc} */ public void setRow(final int row, final double[] array) throws OutOfRangeException, MatrixDimensionMismatchException { MatrixUtils.checkRowIndex(this, row); final int nCols = getColumnDimension(); if (array.length != nCols) { throw new MatrixDimensionMismatchException(1, array.length, 1, nCols); } for (int i = 0; i < nCols; ++i) { setEntry(row, i, array[i]); } } /** {@inheritDoc} */ public double[] getColumn(final int column) throws OutOfRangeException { MatrixUtils.checkColumnIndex(this, column); final int nRows = getRowDimension(); final double[] out = new double[nRows]; for (int i = 0; i < nRows; ++i) { out[i] = getEntry(i, column); } return out; } /** {@inheritDoc} */ public void setColumn(final int column, final double[] array) throws OutOfRangeException, MatrixDimensionMismatchException { MatrixUtils.checkColumnIndex(this, column); final int nRows = getRowDimension(); if (array.length != nRows) { throw new MatrixDimensionMismatchException(array.length, 1, nRows, 1); } for (int i = 0; i < nRows; ++i) { setEntry(i, column, array[i]); } } /** {@inheritDoc

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>} */ public void addToEntry(int row, int column, double increment) throws OutOfRangeException { MatrixUtils.checkMatrixIndex(this, row, column); setEntry(row, column, getEntry(row, column) + increment); } /** {@inheritDoc} */ public void multiplyEntry(int row, int column, double factor) throws OutOfRangeException { MatrixUtils.checkMatrixIndex(this, row, column); setEntry(row, column, getEntry(row, column) * factor); } /** {@inheritDoc} */ public RealMatrix transpose() { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); final RealMatrix out = createMatrix(nCols, nRows); walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() { /** {@inheritDoc} */ @Override public void visit(final int row, final int column, final double value) { out.setEntry(column, row, value); } }); return out; } /** {@inheritDoc} */ public boolean isSquare() { return getColumnDimension() == getRowDimension(); } /** * Returns the number of rows of this matrix. * * @return the number of rows. */ @Override public abstract int getRowDimension(); /** * Returns the number of columns of this matrix. * * @return the number of columns. */ @Override public abstract int getColumnDimension(); /** {@inheritDoc} */ public double getTrace() throws NonSquareMatrixException { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (nRows != nCols) { throw new NonSquareMatrixException(nRows, nCols); } double trace = 0; for (int i = 0; i < nRows; ++i) { trace += getEntry(i, i); } return trace; } /** {@inheritDoc} */ public double[] operate(final double[] v) throws DimensionMismatchException { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.length != nCols) { throw new DimensionMismatchException(v.length, nCols); } final double[] out = new double[nRows]; for (int row = 0; row < nRows

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>; ++row) { double sum = 0; for (int i = 0; i < nCols; ++i) { sum += getEntry(row, i) * v[i]; } out[row] = sum; } return out; } /** {@inheritDoc} */ @Override public RealVector operate(final RealVector v) throws DimensionMismatchException { try { return new ArrayRealVector(operate(((ArrayRealVector) v).getDataRef()), false); } catch (ClassCastException cce) { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.getDimension() != nCols) { throw new DimensionMismatchException(v.getDimension(), nCols); } final double[] out = new double[nRows]; for (int row = 0; row < nRows; ++row) { double sum = 0; for (int i = 0; i < nCols; ++i) { sum += getEntry(row, i) * v.getEntry(i); } out[row] = sum; } return new ArrayRealVector(out, false); } } /** {@inheritDoc} */ public double[] preMultiply(final double[] v) throws DimensionMismatchException { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.length != nRows) { throw new DimensionMismatchException(v.length, nRows); } final double[] out = new double[nCols]; for (int col = 0; col < nCols; ++col) { double sum = 0; for (int i = 0; i < nRows; ++i) { sum += getEntry(i, col) * v[i]; } out[col] = sum; } return out; } /** {@inheritDoc} */ public RealVector preMultiply(final RealVector v) throws DimensionMismatchException { try { return new ArrayRealVector(preMultiply(((ArrayRealVector) v).getDataRef()), false); } catch (ClassCastException cce) { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.getDimension() != nRows) { throw new DimensionMismatchException(v.getDimension(), nRows);

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> } final double[] out = new double[nCols]; for (int col = 0; col < nCols; ++col) { double sum = 0; for (int i = 0; i < nRows; ++i) { sum += getEntry(i, col) * v.getEntry(i); } out[col] = sum; } return new ArrayRealVector(out, false); } } /** {@inheritDoc} */ public double walkInRowOrder(final RealMatrixChangingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int row = 0; row < rows; ++row) { for (int column = 0; column < columns; ++column) { final double oldValue = getEntry(row, column); final double newValue = visitor.visit(row, column, oldValue); setEntry(row, column, newValue); } } return visitor.end(); } /** {@inheritDoc} */ public double walkInRowOrder(final RealMatrixPreservingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int row = 0; row < rows; ++row) { for (int column = 0; column < columns; ++column) { visitor.visit(row, column, getEntry(row, column)); } } return visitor.end(); } /** {@inheritDoc} */ public double walkInRowOrder(final RealMatrixChangingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int row = startRow; row <= endRow; ++row) { for (int column = startColumn; column <= endColumn; ++column) { final double oldValue = getEntry(row, column); final double newValue =

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> visitor.visit(row, column, oldValue); setEntry(row, column, newValue); } } return visitor.end(); } /** {@inheritDoc} */ public double walkInRowOrder(final RealMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int row = startRow; row <= endRow; ++row) { for (int column = startColumn; column <= endColumn; ++column) { visitor.visit(row, column, getEntry(row, column)); } } return visitor.end(); } /** {@inheritDoc} */ public double walkInColumnOrder(final RealMatrixChangingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int column = 0; column < columns; ++column) { for (int row = 0; row < rows; ++row) { final double oldValue = getEntry(row, column); final double newValue = visitor.visit(row, column, oldValue); setEntry(row, column, newValue); } } return visitor.end(); } /** {@inheritDoc} */ public double walkInColumnOrder(final RealMatrixPreservingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int column = 0; column < columns; ++column) { for (int row = 0; row < rows; ++row) { visitor.visit(row, column, getEntry(row, column)); } } return visitor.end(); } /** {@inheritDoc} */ public double walkInColumnOrder(final RealMatrixChangingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException {

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int column = startColumn; column <= endColumn; ++column) { for (int row = startRow; row <= endRow; ++row) { final double oldValue = getEntry(row, column); final double newValue = visitor.visit(row, column, oldValue); setEntry(row, column, newValue); } } return visitor.end(); } /** {@inheritDoc} */ public double walkInColumnOrder(final RealMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int column = startColumn; column <= endColumn; ++column) { for (int row = startRow; row <= endRow; ++row) { visitor.visit(row, column, getEntry(row, column)); } } return visitor.end(); } /** {@inheritDoc} */ public double walkInOptimizedOrder(final RealMatrixChangingVisitor visitor) { return walkInRowOrder(visitor); } /** {@inheritDoc} */ public double walkInOptimizedOrder(final RealMatrixPreservingVisitor visitor) { return walkInRowOrder(visitor); } /** {@inheritDoc} */ public double walkInOptimizedOrder(final RealMatrixChangingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { return walkInRowOrder(visitor, startRow, endRow, startColumn, endColumn); } /** {@inheritDoc} */ public double walkInOptimizedOrder(final RealMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { return walkInRowOrder(visitor, startRow, endRow, startColumn, endColumn); }

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> /** * Get a string representation for this matrix. * @return a string representation for this matrix */ @Override public String toString() { final StringBuilder res = new StringBuilder(); String fullClassName = getClass().getName(); String shortClassName = fullClassName.substring(fullClassName.lastIndexOf('.') + 1); res.append(shortClassName); res.append(DEFAULT_FORMAT.format(this)); return res.toString(); } /** * Returns true iff <code>object</code> is a * <code>RealMatrix</code> instance with the same dimensions as this * and all corresponding matrix entries are equal. * * @param object the object to test equality against. * @return true if object equals this */ @Override public boolean equals(final Object object) { if (object == this ) { return true; } if (object instanceof RealMatrix == false) { return false; } RealMatrix m = (RealMatrix) object; final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (m.getColumnDimension() != nCols || m.getRowDimension() != nRows) { return false; } for (int row = 0; row < nRows; ++row) { for (int col = 0; col < nCols; ++col) { if (getEntry(row, col) != m.getEntry(row, col)) { return false; } } } return true; } /** * Computes a hashcode for the matrix. * * @return hashcode for matrix */ @Override public int hashCode() { int ret = 7; final int nRows = getRowDimension(); final int nCols = getColumnDimension(); ret = ret * 31 + nRows; ret = ret * 31 + nCols; for (int row = 0; row < nRows; ++row) { for (int col = 0; col < nCols; ++col) { ret = ret * 31 + (11 * (row+1) + 17 * (col+1)) * MathUtils.hash(getEntry(row, col)); } } return ret; } /* * Empty implementations of these methods are provided in order to allow for * the use of the @Override tag

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> with Java 1.5. */ /** {@inheritDoc} */ public abstract RealMatrix createMatrix(int rowDimension, int columnDimension) throws NotStrictlyPositiveException; /** {@inheritDoc} */ public abstract RealMatrix copy(); /** {@inheritDoc} */ public abstract double getEntry(int row, int column) throws OutOfRangeException; /** {@inheritDoc} */ public abstract void setEntry(int row, int column, double value) throws OutOfRangeException; }

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>Matrix<T> m) throws MatrixDimensionMismatchException { // safety check checkAdditionCompatible(m); final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final FieldMatrix<T> out = createMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < columnCount; ++col) { out.setEntry(row, col, getEntry(row, col).add(m.getEntry(row, col))); } } return out; } /** {@inheritDoc} */ public FieldMatrix<T> subtract(final FieldMatrix<T> m) throws MatrixDimensionMismatchException { // safety check checkSubtractionCompatible(m); final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final FieldMatrix<T> out = createMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < columnCount; ++col) { out.setEntry(row, col, getEntry(row, col).subtract(m.getEntry(row, col))); } } return out; } /** {@inheritDoc} */ public FieldMatrix<T> scalarAdd(final T d) { final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final FieldMatrix<T> out = createMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < columnCount; ++col) { out.setEntry(row, col, getEntry(row, col).add(d)); } } return out; } /** {@inheritDoc} */ public FieldMatrix<T> scalarMultiply(final T d) { final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final FieldMatrix<T> out = createMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < columnCount; ++col) { out.setEntry(row, col, getEntry(row, col).multiply(d)); } }

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> return out; } /** {@inheritDoc} */ public FieldMatrix<T> multiply(final FieldMatrix<T> m) throws DimensionMismatchException { // safety check checkMultiplicationCompatible(m); final int nRows = getRowDimension(); final int nCols = m.getColumnDimension(); final int nSum = getColumnDimension(); final FieldMatrix<T> out = createMatrix(nRows, nCols); for (int row = 0; row < nRows; ++row) { for (int col = 0; col < nCols; ++col) { T sum = field.getZero(); for (int i = 0; i < nSum; ++i) { sum = sum.add(getEntry(row, i).multiply(m.getEntry(i, col))); } out.setEntry(row, col, sum); } } return out; } /** {@inheritDoc} */ public FieldMatrix<T> preMultiply(final FieldMatrix<T> m) throws DimensionMismatchException { return m.multiply(this); } /** {@inheritDoc} */ public FieldMatrix<T> power(final int p) throws NonSquareMatrixException, NotPositiveException { if (p < 0) { throw new NotPositiveException(p); } if (!isSquare()) { throw new NonSquareMatrixException(getRowDimension(), getColumnDimension()); } if (p == 0) { return MatrixUtils.createFieldIdentityMatrix(this.getField(), this.getRowDimension()); } if (p == 1) { return this.copy(); } final int power = p - 1; /* * Only log_2(p) operations is used by doing as follows: * 5^214 = 5^128 * 5^64 * 5^16 * 5^4 * 5^2 * * In general, the same approach is used for A^p. */ final char[] binaryRepresentation = Integer.toBinaryString(power) .toCharArray(); final ArrayList<Integer> nonZeroPositions = new ArrayList<Integer>(); for (int i = 0; i < binaryRepresentation.length; ++i) { if (binaryRepresentation[i] == '1') { final int pos = binaryRepresentation.length - i - 1;

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> nonZeroPositions.add(pos); } } ArrayList<FieldMatrix<T>> results = new ArrayList<FieldMatrix<T>>( binaryRepresentation.length); results.add(0, this.copy()); for (int i = 1; i < binaryRepresentation.length; ++i) { final FieldMatrix<T> s = results.get(i - 1); final FieldMatrix<T> r = s.multiply(s); results.add(i, r); } FieldMatrix<T> result = this.copy(); for (Integer i : nonZeroPositions) { result = result.multiply(results.get(i)); } return result; } /** {@inheritDoc} */ public T[][] getData() { final T[][] data = buildArray(field, getRowDimension(), getColumnDimension()); for (int i = 0; i < data.length; ++i) { final T[] dataI = data[i]; for (int j = 0; j < dataI.length; ++j) { dataI[j] = getEntry(i, j); } } return data; } /** {@inheritDoc} */ public FieldMatrix<T> getSubMatrix(final int startRow, final int endRow, final int startColumn, final int endColumn) throws NumberIsTooSmallException, OutOfRangeException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); final FieldMatrix<T> subMatrix = createMatrix(endRow - startRow + 1, endColumn - startColumn + 1); for (int i = startRow; i <= endRow; ++i) { for (int j = startColumn; j <= endColumn; ++j) { subMatrix.setEntry(i - startRow, j - startColumn, getEntry(i, j)); } } return subMatrix; } /** {@inheritDoc} */ public FieldMatrix<T> getSubMatrix(final int[] selectedRows, final int[] selectedColumns) throws NoDataException, NullArgumentException, OutOfRangeException { // safety checks checkSubMatrixIndex(selectedRows, selectedColumns); // copy entries final FieldMatrix<T> subMatrix = createMatrix(selectedRows.length, selectedColumns.length); subMatrix.walkInOptimizedOrder(new DefaultFieldMatrixChanging

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>Visitor<T>(field.getZero()) { /** {@inheritDoc} */ @Override public T visit(final int row, final int column, final T value) { return getEntry(selectedRows[row], selectedColumns[column]); } }); return subMatrix; } /** {@inheritDoc} */ public void copySubMatrix(final int startRow, final int endRow, final int startColumn, final int endColumn, final T[][] destination) throws MatrixDimensionMismatchException, NumberIsTooSmallException, OutOfRangeException{ // safety checks checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); final int rowsCount = endRow + 1 - startRow; final int columnsCount = endColumn + 1 - startColumn; if ((destination.length < rowsCount) || (destination[0].length < columnsCount)) { throw new MatrixDimensionMismatchException(destination.length, destination[0].length, rowsCount, columnsCount); } // copy entries walkInOptimizedOrder(new DefaultFieldMatrixPreservingVisitor<T>(field.getZero()) { /** Initial row index. */ private int startRow; /** Initial column index. */ private int startColumn; /** {@inheritDoc} */ @Override public void start(final int rows, final int columns, final int startRow, final int endRow, final int startColumn, final int endColumn) { this.startRow = startRow; this.startColumn = startColumn; } /** {@inheritDoc} */ @Override public void visit(final int row, final int column, final T value) { destination[row - startRow][column - startColumn] = value; } }, startRow, endRow, startColumn, endColumn); } /** {@inheritDoc} */ public void copySubMatrix(int[] selectedRows, int[] selectedColumns, T[][] destination) throws MatrixDimensionMismatchException, NoDataException, NullArgumentException, OutOfRangeException { // safety checks checkSubMatrixIndex(selectedRows, selectedColumns); if ((destination.length < selectedRows.length) || (destination[0].length < selectedColumns.length)) { throw new MatrixDimensionMismatchException(destination.length, destination[0].length, selectedRows.length, selectedColumns.length); } // copy entries for (

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>int i = 0; i < selectedRows.length; i++) { final T[] destinationI = destination[i]; for (int j = 0; j < selectedColumns.length; j++) { destinationI[j] = getEntry(selectedRows[i], selectedColumns[j]); } } } /** {@inheritDoc} */ public void setSubMatrix(final T[][] subMatrix, final int row, final int column) throws DimensionMismatchException, OutOfRangeException, NoDataException, NullArgumentException { if (subMatrix == null) { throw new NullArgumentException(); } final int nRows = subMatrix.length; if (nRows == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); } final int nCols = subMatrix[0].length; if (nCols == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN); } for (int r = 1; r < nRows; ++r) { if (subMatrix[r].length != nCols) { throw new DimensionMismatchException(nCols, subMatrix[r].length); } } checkRowIndex(row); checkColumnIndex(column); checkRowIndex(nRows + row - 1); checkColumnIndex(nCols + column - 1); for (int i = 0; i < nRows; ++i) { for (int j = 0; j < nCols; ++j) { setEntry(row + i, column + j, subMatrix[i][j]); } } } /** {@inheritDoc} */ public FieldMatrix<T> getRowMatrix(final int row) throws OutOfRangeException { checkRowIndex(row); final int nCols = getColumnDimension(); final FieldMatrix<T> out = createMatrix(1, nCols); for (int i = 0; i < nCols; ++i) { out.setEntry(0, i, getEntry(row, i)); } return out; } /** {@inheritDoc} */ public void setRowMatrix(final int row, final FieldMatrix<T> matrix) throws OutOfRangeException, MatrixDimensionMismatchException { checkRowIndex(row); final int nCols = getColumnDimension(); if ((matrix.getRowDimension()

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> != 1) || (matrix.getColumnDimension() != nCols)) { throw new MatrixDimensionMismatchException(matrix.getRowDimension(), matrix.getColumnDimension(), 1, nCols); } for (int i = 0; i < nCols; ++i) { setEntry(row, i, matrix.getEntry(0, i)); } } /** {@inheritDoc} */ public FieldMatrix<T> getColumnMatrix(final int column) throws OutOfRangeException { checkColumnIndex(column); final int nRows = getRowDimension(); final FieldMatrix<T> out = createMatrix(nRows, 1); for (int i = 0; i < nRows; ++i) { out.setEntry(i, 0, getEntry(i, column)); } return out; } /** {@inheritDoc} */ public void setColumnMatrix(final int column, final FieldMatrix<T> matrix) throws OutOfRangeException, MatrixDimensionMismatchException { checkColumnIndex(column); final int nRows = getRowDimension(); if ((matrix.getRowDimension() != nRows) || (matrix.getColumnDimension() != 1)) { throw new MatrixDimensionMismatchException(matrix.getRowDimension(), matrix.getColumnDimension(), nRows, 1); } for (int i = 0; i < nRows; ++i) { setEntry(i, column, matrix.getEntry(i, 0)); } } /** {@inheritDoc} */ public FieldVector<T> getRowVector(final int row) throws OutOfRangeException { return new ArrayFieldVector<T>(field, getRow(row), false); } /** {@inheritDoc} */ public void setRowVector(final int row, final FieldVector<T> vector) throws OutOfRangeException, MatrixDimensionMismatchException { checkRowIndex(row); final int nCols = getColumnDimension(); if (vector.getDimension() != nCols) { throw new MatrixDimensionMismatchException(1, vector.getDimension(), 1, nCols); } for (int i = 0; i < nCols; ++i) { setEntry(row, i, vector.getEntry(i)); } } /** {@inheritDoc} */ public FieldVector<T> getColumnVector(final int column) throws OutOfRangeException

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> { return new ArrayFieldVector<T>(field, getColumn(column), false); } /** {@inheritDoc} */ public void setColumnVector(final int column, final FieldVector<T> vector) throws OutOfRangeException, MatrixDimensionMismatchException { checkColumnIndex(column); final int nRows = getRowDimension(); if (vector.getDimension() != nRows) { throw new MatrixDimensionMismatchException(vector.getDimension(), 1, nRows, 1); } for (int i = 0; i < nRows; ++i) { setEntry(i, column, vector.getEntry(i)); } } /** {@inheritDoc} */ public T[] getRow(final int row) throws OutOfRangeException { checkRowIndex(row); final int nCols = getColumnDimension(); final T[] out = buildArray(field, nCols); for (int i = 0; i < nCols; ++i) { out[i] = getEntry(row, i); } return out; } /** {@inheritDoc} */ public void setRow(final int row, final T[] array) throws OutOfRangeException, MatrixDimensionMismatchException { checkRowIndex(row); final int nCols = getColumnDimension(); if (array.length != nCols) { throw new MatrixDimensionMismatchException(1, array.length, 1, nCols); } for (int i = 0; i < nCols; ++i) { setEntry(row, i, array[i]); } } /** {@inheritDoc} */ public T[] getColumn(final int column) throws OutOfRangeException { checkColumnIndex(column); final int nRows = getRowDimension(); final T[] out = buildArray(field, nRows); for (int i = 0; i < nRows; ++i) { out[i] = getEntry(i, column); } return out; } /** {@inheritDoc} */ public void setColumn(final int column, final T[] array) throws OutOfRangeException, MatrixDimensionMismatchException { checkColumnIndex(column); final int nRows = getRowDimension(); if (array.length != nRows) { throw new MatrixDimensionMismatchException(array.length, 1, nRows, 1); }

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> for (int i = 0; i < nRows; ++i) { setEntry(i, column, array[i]); } } /** {@inheritDoc} */ public abstract T getEntry(int row, int column) throws OutOfRangeException; /** {@inheritDoc} */ public abstract void setEntry(int row, int column, T value) throws OutOfRangeException; /** {@inheritDoc} */ public abstract void addToEntry(int row, int column, T increment) throws OutOfRangeException; /** {@inheritDoc} */ public abstract void multiplyEntry(int row, int column, T factor) throws OutOfRangeException; /** {@inheritDoc} */ public FieldMatrix<T> transpose() { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); final FieldMatrix<T> out = createMatrix(nCols, nRows); walkInOptimizedOrder(new DefaultFieldMatrixPreservingVisitor<T>(field.getZero()) { /** {@inheritDoc} */ @Override public void visit(final int row, final int column, final T value) { out.setEntry(column, row, value); } }); return out; } /** {@inheritDoc} */ public boolean isSquare() { return getColumnDimension() == getRowDimension(); } /** {@inheritDoc} */ public abstract int getRowDimension(); /** {@inheritDoc} */ public abstract int getColumnDimension(); /** {@inheritDoc} */ public T getTrace() throws NonSquareMatrixException { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (nRows != nCols) { throw new NonSquareMatrixException(nRows, nCols); } T trace = field.getZero(); for (int i = 0; i < nRows; ++i) { trace = trace.add(getEntry(i, i)); } return trace; } /** {@inheritDoc} */ public T[] operate(final T[] v) throws DimensionMismatchException { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.length != nCols) { throw new DimensionMismatchException(v.length, nCols); } final T[] out = buildArray(field, nRows); for (int row = 0; row < nRows; ++row) {

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> T sum = field.getZero(); for (int i = 0; i < nCols; ++i) { sum = sum.add(getEntry(row, i).multiply(v[i])); } out[row] = sum; } return out; } /** {@inheritDoc} */ public FieldVector<T> operate(final FieldVector<T> v) throws DimensionMismatchException { try { return new ArrayFieldVector<T>(field, operate(((ArrayFieldVector<T>) v).getDataRef()), false); } catch (ClassCastException cce) { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.getDimension() != nCols) { throw new DimensionMismatchException(v.getDimension(), nCols); } final T[] out = buildArray(field, nRows); for (int row = 0; row < nRows; ++row) { T sum = field.getZero(); for (int i = 0; i < nCols; ++i) { sum = sum.add(getEntry(row, i).multiply(v.getEntry(i))); } out[row] = sum; } return new ArrayFieldVector<T>(field, out, false); } } /** {@inheritDoc} */ public T[] preMultiply(final T[] v) throws DimensionMismatchException { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.length != nRows) { throw new DimensionMismatchException(v.length, nRows); } final T[] out = buildArray(field, nCols); for (int col = 0; col < nCols; ++col) { T sum = field.getZero(); for (int i = 0; i < nRows; ++i) { sum = sum.add(getEntry(i, col).multiply(v[i])); } out[col] = sum; } return out; } /** {@inheritDoc} */ public FieldVector<T> preMultiply(final FieldVector<T> v) throws DimensionMismatchException { try { return new ArrayFieldVector<T>(field, preMultiply(((ArrayFieldVector<T>) v).getDataRef()), false); } catch (ClassCastException cce) { final int nRows =

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> getRowDimension(); final int nCols = getColumnDimension(); if (v.getDimension() != nRows) { throw new DimensionMismatchException(v.getDimension(), nRows); } final T[] out = buildArray(field, nCols); for (int col = 0; col < nCols; ++col) { T sum = field.getZero(); for (int i = 0; i < nRows; ++i) { sum = sum.add(getEntry(i, col).multiply(v.getEntry(i))); } out[col] = sum; } return new ArrayFieldVector<T>(field, out, false); } } /** {@inheritDoc} */ public T walkInRowOrder(final FieldMatrixChangingVisitor<T> visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int row = 0; row < rows; ++row) { for (int column = 0; column < columns; ++column) { final T oldValue = getEntry(row, column); final T newValue = visitor.visit(row, column, oldValue); setEntry(row, column, newValue); } } return visitor.end(); } /** {@inheritDoc} */ public T walkInRowOrder(final FieldMatrixPreservingVisitor<T> visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int row = 0; row < rows; ++row) { for (int column = 0; column < columns; ++column) { visitor.visit(row, column, getEntry(row, column)); } } return visitor.end(); } /** {@inheritDoc} */ public T walkInRowOrder(final FieldMatrixChangingVisitor<T> visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws NumberIsTooSmallException, OutOfRangeException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow,

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> startColumn, endColumn); for (int row = startRow; row <= endRow; ++row) { for (int column = startColumn; column <= endColumn; ++column) { final T oldValue = getEntry(row, column); final T newValue = visitor.visit(row, column, oldValue); setEntry(row, column, newValue); } } return visitor.end(); } /** {@inheritDoc} */ public T walkInRowOrder(final FieldMatrixPreservingVisitor<T> visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws NumberIsTooSmallException, OutOfRangeException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int row = startRow; row <= endRow; ++row) { for (int column = startColumn; column <= endColumn; ++column) { visitor.visit(row, column, getEntry(row, column)); } } return visitor.end(); } /** {@inheritDoc} */ public T walkInColumnOrder(final FieldMatrixChangingVisitor<T> visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int column = 0; column < columns; ++column) { for (int row = 0; row < rows; ++row) { final T oldValue = getEntry(row, column); final T newValue = visitor.visit(row, column, oldValue); setEntry(row, column, newValue); } } return visitor.end(); } /** {@inheritDoc} */ public T walkInColumnOrder(final FieldMatrixPreservingVisitor<T> visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int column = 0; column < columns; ++column) { for (int row = 0; row < rows; ++row) { visitor.visit(row, column, getEntry(row, column)); } }

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> return visitor.end(); } /** {@inheritDoc} */ public T walkInColumnOrder(final FieldMatrixChangingVisitor<T> visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws NumberIsTooSmallException, OutOfRangeException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int column = startColumn; column <= endColumn; ++column) { for (int row = startRow; row <= endRow; ++row) { final T oldValue = getEntry(row, column); final T newValue = visitor.visit(row, column, oldValue); setEntry(row, column, newValue); } } return visitor.end(); } /** {@inheritDoc} */ public T walkInColumnOrder(final FieldMatrixPreservingVisitor<T> visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws NumberIsTooSmallException, OutOfRangeException{ checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int column = startColumn; column <= endColumn; ++column) { for (int row = startRow; row <= endRow; ++row) { visitor.visit(row, column, getEntry(row, column)); } } return visitor.end(); } /** {@inheritDoc} */ public T walkInOptimizedOrder(final FieldMatrixChangingVisitor<T> visitor) { return walkInRowOrder(visitor); } /** {@inheritDoc} */ public T walkInOptimizedOrder(final FieldMatrixPreservingVisitor<T> visitor) { return walkInRowOrder(visitor); } /** {@inheritDoc} */ public T walkInOptimizedOrder(final FieldMatrixChangingVisitor<T> visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws NumberIsTooSmallException, OutOfRangeException { return walkInRowOrder(visitor, startRow, endRow, startColumn, endColumn); } /** {@inheritDoc} */ public T walkInOptimizedOrder(final

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> FieldMatrixPreservingVisitor<T> visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws NumberIsTooSmallException, OutOfRangeException { return walkInRowOrder(visitor, startRow, endRow, startColumn, endColumn); } /** * Get a string representation for this matrix. * @return a string representation for this matrix */ @Override public String toString() { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); final StringBuffer res = new StringBuffer(); String fullClassName = getClass().getName(); String shortClassName = fullClassName.substring(fullClassName.lastIndexOf('.') + 1); res.append(shortClassName).append("{"); for (int i = 0; i < nRows; ++i) { if (i > 0) { res.append(","); } res.append("{"); for (int j = 0; j < nCols; ++j) { if (j > 0) { res.append(","); } res.append(getEntry(i, j)); } res.append("}"); } res.append("}"); return res.toString(); } /** * Returns true iff <code>object</code> is a * <code>FieldMatrix</code> instance with the same dimensions as this * and all corresponding matrix entries are equal. * * @param object the object to test equality against. * @return true if object equals this */ @Override public boolean equals(final Object object) { if (object == this ) { return true; } if (object instanceof FieldMatrix<?> == false) { return false; } FieldMatrix<?> m = (FieldMatrix<?>) object; final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (m.getColumnDimension() != nCols || m.getRowDimension() != nRows) { return false; } for (int row = 0; row < nRows; ++row) { for (int col = 0; col < nCols; ++col) { if (!getEntry(row, col).equals(m.getEntry(row, col))) { return false; } } } return true; } /** * Computes a hashcode for the matrix.

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> * * @return hashcode for matrix */ @Override public int hashCode() { int ret = 322562; final int nRows = getRowDimension(); final int nCols = getColumnDimension(); ret = ret * 31 + nRows; ret = ret * 31 + nCols; for (int row = 0; row < nRows; ++row) { for (int col = 0; col < nCols; ++col) { ret = ret * 31 + (11 * (row+1) + 17 * (col+1)) * getEntry(row, col).hashCode(); } } return ret; } /** * Check if a row index is valid. * * @param row Row index to check. * @throws OutOfRangeException if {@code index} is not valid. */ protected void checkRowIndex(final int row) throws OutOfRangeException { if (row < 0 || row >= getRowDimension()) { throw new OutOfRangeException(LocalizedFormats.ROW_INDEX, row, 0, getRowDimension() - 1); } } /** * Check if a column index is valid. * * @param column Column index to check. * @throws OutOfRangeException if {@code index} is not valid. */ protected void checkColumnIndex(final int column) throws OutOfRangeException { if (column < 0 || column >= getColumnDimension()) { throw new OutOfRangeException(LocalizedFormats.COLUMN_INDEX, column, 0, getColumnDimension() - 1); } } /** * Check if submatrix ranges indices are valid. * Rows and columns are indicated counting from 0 to n-1. * * @param startRow Initial row index. * @param endRow Final row index. * @param startColumn Initial column index. * @param endColumn Final column index. * @throws OutOfRangeException if the indices are not valid. * @throws NumberIsTooSmallException if {@code endRow < startRow} or * {@code endColumn < startColumn}. */ protected void checkSubMatrixIndex(final int startRow, final int endRow, final int startColumn, final int endColumn) throws NumberIsTooSmallException, OutOfRangeException

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>. */ public StringBuffer format(RealMatrix matrix, StringBuffer toAppendTo, FieldPosition pos) { pos.setBeginIndex(0); pos.setEndIndex(0); // format prefix toAppendTo.append(prefix); // format rows final int rows = matrix.getRowDimension(); for (int i = 0; i < rows; ++i) { toAppendTo.append(rowPrefix); for (int j = 0; j < matrix.getColumnDimension(); ++j) { if (j > 0) { toAppendTo.append(columnSeparator); } CompositeFormat.formatDouble(matrix.getEntry(i, j), format, toAppendTo, pos); } toAppendTo.append(rowSuffix); if (i < rows - 1) { toAppendTo.append(rowSeparator); } } // format suffix toAppendTo.append(suffix); return toAppendTo; } /** * Parse a string to produce a {@link RealMatrix} object. * * @param source String to parse. * @return the parsed {@link RealMatrix} object. * @throws MathParseException if the beginning of the specified string * cannot be parsed. */ public RealMatrix parse(String source) { final ParsePosition parsePosition = new ParsePosition(0); final RealMatrix result = parse(source, parsePosition); if (parsePosition.getIndex() == 0) { throw new MathParseException(source, parsePosition.getErrorIndex(), Array2DRowRealMatrix.class); } return result; } /** * Parse a string to produce a {@link RealMatrix} object. * * @param source String to parse. * @param pos input/ouput parsing parameter. * @return the parsed {@link RealMatrix} object. */ public RealMatrix parse(String source, ParsePosition pos) { int initialIndex = pos.getIndex(); final String trimmedPrefix = prefix.trim(); final String trimmedSuffix = suffix.trim(); final String trimmedRowPrefix = rowPrefix.trim(); final String trimmedRowSuffix = rowSuffix.trim(); final String trimmedColumnSeparator = columnSeparator.trim(); final String trimmedRowSeparator = rowSeparator.trim(); // parse prefix CompositeFormat.parseAndIgnoreWhitespace(source, pos); if (!CompositeFormat.parseFixedstring(source

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> applying a function ...) on each element in turn. The {@code map} * versions create a new vector to hold the result and do not change the instance. * The {@code mapToSelf} version uses the instance itself to store the * results, so the instance is changed by this method. In all cases, the result * vector is returned by the methods, allowing the <i>fluent API</i> * style, like this: * </p> * <pre> * RealVector result = v.mapAddToSelf(3.4).mapToSelf(new Tan()).mapToSelf(new Power(2.3)); * </pre> * * @version $Id$ * @since 2.1 */ public abstract class RealVector { /** * Returns the size of the vector. * * @return the size of this vector. */ public abstract int getDimension(); /** * Return the entry at the specified index. * * @param index Index location of entry to be fetched. * @return the vector entry at {@code index}. * @throws OutOfRangeException if the index is not valid. * @see #setEntry(int, double) */ public abstract double getEntry(int index) throws OutOfRangeException; /** * Set a single element. * * @param index element index. * @param value new value for the element. * @throws OutOfRangeException if the index is not valid. * @see #getEntry(int) */ public abstract void setEntry(int index, double value) throws OutOfRangeException; /** * Change an entry at the specified index. * * @param index Index location of entry to be set. * @param increment Value to add to the vector entry. * @throws OutOfRangeException if the index is not valid. * @since 3.0 */ public void addToEntry(int index, double increment) throws OutOfRangeException { setEntry(index, getEntry(index) + increment); } /** * Construct a new vector by appending a vector to this vector. * * @param v vector to append to this one. * @return a new vector. */ public abstract RealVector append(RealVector v); /** * Construct a new vector by appending a double to this vector. *

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>. */ protected void checkIndex(final int index) throws OutOfRangeException { if (index < 0 || index >= getDimension()) { throw new OutOfRangeException(LocalizedFormats.INDEX, index, 0, getDimension() - 1); } } /** * Checks that the indices of a subvector are valid. * * @param start the index of the first entry of the subvector * @param end the index of the last entry of the subvector (inclusive) * @throws OutOfRangeException if {@code start} of {@code end} are not valid * @throws NumberIsTooSmallException if {@code end < start} * @since 3.1 */ protected void checkIndices(final int start, final int end) throws NumberIsTooSmallException, OutOfRangeException { final int dim = getDimension(); if ((start < 0) || (start >= dim)) { throw new OutOfRangeException(LocalizedFormats.INDEX, start, 0, dim - 1); } if ((end < 0) || (end >= dim)) { throw new OutOfRangeException(LocalizedFormats.INDEX, end, 0, dim - 1); } if (end < start) { // TODO Use more specific error message throw new NumberIsTooSmallException(LocalizedFormats.INITIAL_ROW_AFTER_FINAL_ROW, end, start, false); } } /** * Compute the sum of this vector and {@code v}. * Returns a new vector. Does not change instance data. * * @param v Vector to be added. * @return {@code this} + {@code v}. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} vector. */ public RealVector add(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v); RealVector result = v.copy(); Iterator<Entry> it = iterator(); while (it.hasNext()) { final Entry e = it.next(); final int index = e.getIndex(); result.setEntry(index, e.getValue() + result.getEntry(index)); } return result; } /** * Subtract {@code v} from this vector. * Returns a new vector. Does not change instance data. * * @param v Vector

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> to be subtracted. * @return {@code this} - {@code v}. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} vector. */ public RealVector subtract(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v); RealVector result = v.mapMultiply(-1d); Iterator<Entry> it = iterator(); while (it.hasNext()) { final Entry e = it.next(); final int index = e.getIndex(); result.setEntry(index, e.getValue() + result.getEntry(index)); } return result; } /** * Add a value to each entry. * Returns a new vector. Does not change instance data. * * @param d Value to be added to each entry. * @return {@code this} + {@code d}. */ public RealVector mapAdd(double d) { return copy().mapAddToSelf(d); } /** * Add a value to each entry. * The instance is changed in-place. * * @param d Value to be added to each entry. * @return {@code this}. */ public RealVector mapAddToSelf(double d) { if (d != 0) { return mapToSelf(FunctionUtils.fix2ndArgument(new Add(), d)); } return this; } /** * Returns a (deep) copy of this vector. * * @return a vector copy. */ public abstract RealVector copy(); /** * Compute the dot product of this vector with {@code v}. * * @param v Vector with which dot product should be computed * @return the scalar dot product between this instance and {@code v}. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} vector. */ public double dotProduct(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v); double d = 0; final int n = getDimension(); for (int i = 0; i < n; i++) { d += getEntry(i) * v.getEntry(i); } return d; } /** * Computes the cosine of the angle between this vector and the * argument. * * @param v Vector. * @return the cosine of

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>issues.apache.org/jira/browse/MATH-803?focusedCommentId=13399150#comment-13399150">MATH-803</a>. * Uses of this method involving sparse implementations of * {@link RealVector} might lead to wrong results. Since there is no * satisfactory correction to this bug, this method is deprecated. Users who * want to preserve this feature are advised to implement * {@link RealVectorPreservingVisitor} (possibly ignoring corner cases for * the sake of efficiency). */ @Deprecated public abstract RealVector ebeMultiply(RealVector v) throws DimensionMismatchException; /** * Distance between two vectors. * <p>This method computes the distance consistent with the * L<sub>2</sub> norm, i.e. the square root of the sum of * element differences, or Euclidean distance.</p> * * @param v Vector to which distance is requested. * @return the distance between two vectors. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} vector. * @see #getL1Distance(RealVector) * @see #getLInfDistance(RealVector) * @see #getNorm() */ public double getDistance(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v); double d = 0; Iterator<Entry> it = iterator(); while (it.hasNext()) { final Entry e = it.next(); final double diff = e.getValue() - v.getEntry(e.getIndex()); d += diff * diff; } return FastMath.sqrt(d); } /** * Returns the L<sub>2</sub> norm of the vector. * <p>The L<sub>2</sub> norm is the root of the sum of * the squared elements.</p> * * @return the norm. * @see #getL1Norm() * @see #getLInfNorm() * @see #getDistance(RealVector) */ public double getNorm() { double sum = 0; Iterator<Entry> it = iterator(); while (it.hasNext()) { final Entry e = it.next(); final double value = e.getValue(); sum += value * value;

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> } return FastMath.sqrt(sum); } /** * Returns the L<sub>1</sub> norm of the vector. * <p>The L<sub>1</sub> norm is the sum of the absolute * values of the elements.</p> * * @return the norm. * @see #getNorm() * @see #getLInfNorm() * @see #getL1Distance(RealVector) */ public double getL1Norm() { double norm = 0; Iterator<Entry> it = iterator(); while (it.hasNext()) { final Entry e = it.next(); norm += FastMath.abs(e.getValue()); } return norm; } /** * Returns the L<sub>&infin;</sub> norm of the vector. * <p>The L<sub>&infin;</sub> norm is the max of the absolute * values of the elements.</p> * * @return the norm. * @see #getNorm() * @see #getL1Norm() * @see #getLInfDistance(RealVector) */ public double getLInfNorm() { double norm = 0; Iterator<Entry> it = iterator(); while (it.hasNext()) { final Entry e = it.next(); norm = FastMath.max(norm, FastMath.abs(e.getValue())); } return norm; } /** * Distance between two vectors. * <p>This method computes the distance consistent with * L<sub>1</sub> norm, i.e. the sum of the absolute values of * the elements differences.</p> * * @param v Vector to which distance is requested. * @return the distance between two vectors. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} vector. */ public double getL1Distance(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v); double d = 0; Iterator<Entry> it = iterator(); while (it.hasNext()) { final Entry e = it.next(); d += FastMath.abs(e.getValue() - v.getEntry(e.getIndex())); } return d; } /** * Distance between two vectors. * <p>This method

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> computes the distance consistent with * L<sub>&infin;</sub> norm, i.e. the max of the absolute values of * element differences.</p> * * @param v Vector to which distance is requested. * @return the distance between two vectors. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} vector. * @see #getDistance(RealVector) * @see #getL1Distance(RealVector) * @see #getLInfNorm() */ public double getLInfDistance(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v); double d = 0; Iterator<Entry> it = iterator(); while (it.hasNext()) { final Entry e = it.next(); d = FastMath.max(FastMath.abs(e.getValue() - v.getEntry(e.getIndex())), d); } return d; } /** * Get the index of the minimum entry. * * @return the index of the minimum entry or -1 if vector length is 0 * or all entries are {@code NaN}. */ public int getMinIndex() { int minIndex = -1; double minValue = Double.POSITIVE_INFINITY; Iterator<Entry> iterator = iterator(); while (iterator.hasNext()) { final Entry entry = iterator.next(); if (entry.getValue() <= minValue) { minIndex = entry.getIndex(); minValue = entry.getValue(); } } return minIndex; } /** * Get the value of the minimum entry. * * @return the value of the minimum entry or {@code NaN} if all * entries are {@code NaN}. */ public double getMinValue() { final int minIndex = getMinIndex(); return minIndex < 0 ? Double.NaN : getEntry(minIndex); } /** * Get the index of the maximum entry. * * @return the index of the maximum entry or -1 if vector length is 0 * or all entries are {@code NaN} */ public int getMaxIndex() { int maxIndex = -1; double maxValue = Double.NEGATIVE_INFINITY; Iterator<Entry> iterator = iterator(); while (iterator.hasNext()) { final Entry entry = iterator.next(); if (entry.getValue

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>() >= maxValue) { maxIndex = entry.getIndex(); maxValue = entry.getValue(); } } return maxIndex; } /** * Get the value of the maximum entry. * * @return the value of the maximum entry or {@code NaN} if all * entries are {@code NaN}. */ public double getMaxValue() { final int maxIndex = getMaxIndex(); return maxIndex < 0 ? Double.NaN : getEntry(maxIndex); } /** * Multiply each entry by the argument. Returns a new vector. * Does not change instance data. * * @param d Multiplication factor. * @return {@code this} * {@code d}. */ public RealVector mapMultiply(double d) { return copy().mapMultiplyToSelf(d); } /** * Multiply each entry. * The instance is changed in-place. * * @param d Multiplication factor. * @return {@code this}. */ public RealVector mapMultiplyToSelf(double d){ return mapToSelf(FunctionUtils.fix2ndArgument(new Multiply(), d)); } /** * Subtract a value from each entry. Returns a new vector. * Does not change instance data. * * @param d Value to be subtracted. * @return {@code this} - {@code d}. */ public RealVector mapSubtract(double d) { return copy().mapSubtractToSelf(d); } /** * Subtract a value from each entry. * The instance is changed in-place. * * @param d Value to be subtracted. * @return {@code this}. */ public RealVector mapSubtractToSelf(double d){ return mapAddToSelf(-d); } /** * Divide each entry by the argument. Returns a new vector. * Does not change instance data. * * @param d Value to divide by. * @return {@code this} / {@code d}. */ public RealVector mapDivide(double d) { return copy().mapDivideToSelf(d); } /** * Divide each entry by the argument. * The instance is changed in-place. * * @param d Value to divide by. * @return {@code this}. */ public RealVector mapDivideToSelf(double d){ return mapToSelf(Function

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>Utils.fix2ndArgument(new Divide(), d)); } /** * Compute the outer product. * * @param v Vector with which outer product should be computed. * @return the matrix outer product between this instance and {@code v}. */ public RealMatrix outerProduct(RealVector v) { final int m = this.getDimension(); final int n = v.getDimension(); final RealMatrix product; if (v instanceof SparseRealVector || this instanceof SparseRealVector) { product = new OpenMapRealMatrix(m, n); } else { product = new Array2DRowRealMatrix(m, n); } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { product.setEntry(i, j, this.getEntry(i) * v.getEntry(j)); } } return product; } /** * Find the orthogonal projection of this vector onto another vector. * * @param v vector onto which instance must be projected. * @return projection of the instance onto {@code v}. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} vector. * @throws MathArithmeticException if {@code this} or {@code v} is the null * vector */ public RealVector projection(final RealVector v) throws DimensionMismatchException, MathArithmeticException { final double norm2 = v.dotProduct(v); if (norm2 == 0.0) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); } return v.mapMultiply(dotProduct(v) / v.dotProduct(v)); } /** * Set all elements to a single value. * * @param value Single value to set for all elements. */ public void set(double value) { Iterator<Entry> it = iterator(); while (it.hasNext()) { final Entry e = it.next(); e.setValue(value); } } /** * Convert the vector to an array of {@code double}s. * The array is independent from this vector data: the elements * are copied. * * @return an array containing a copy of the vector elements. */ public double[] toArray() { int dim = getDimension(); double

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>[] values = new double[dim]; for (int i = 0; i < dim; i++) { values[i] = getEntry(i); } return values; } /** * Creates a unit vector pointing in the direction of this vector. * The instance is not changed by this method. * * @return a unit vector pointing in direction of this vector. * @throws MathArithmeticException if the norm is zero. */ public RealVector unitVector() throws MathArithmeticException { final double norm = getNorm(); if (norm == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); } return mapDivide(norm); } /** * Converts this vector into a unit vector. * The instance itself is changed by this method. * * @throws MathArithmeticException if the norm is zero. */ public void unitize() throws MathArithmeticException { final double norm = getNorm(); if (norm == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); } mapDivideToSelf(getNorm()); } /** * Create a sparse iterator over the vector, which may omit some entries. * Specialized implementations may choose to not iterate over all * dimensions, either because those values are unset, or are equal * to defaultValue(), or are small enough to be ignored for the * purposes of iteration. No guarantees are made about order of iteration. * In dense implementations, this method will often delegate to * {@link #iterator()}. * * <p>Note: derived classes are required to return an {@link Iterator} that * returns non-null {@link Entry} objects as long as {@link Iterator#hasNext()} * returns {@code true}.</p> * * @return a sparse iterator. * @deprecated As of 3.1, this method is deprecated, because its interface * is too confusing (see * <a href="https://issues.apache.org/jira/browse/MATH-875">JIRA MATH-875</a>). * This method will be completely removed in 4.0. */ @Deprecated public Iterator<Entry> sparseIterator() { return new SparseEntryIterator(); } /** * Generic dense iterator. Iteration is in increasing order * of the vector index. * * <p>Note: derived classes are required

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> * @param a Coefficient of {@code this}. * @param b Coefficient of {@code y}. * @param y Vector with which {@code this} is linearly combined. * @return a vector containing {@code a * this[i] + b * y[i]} for all * {@code i}. * @throws DimensionMismatchException if {@code y} is not the same size as * {@code this} vector. */ public RealVector combine(double a, double b, RealVector y) throws DimensionMismatchException { return copy().combineToSelf(a, b, y); } /** * Updates {@code this} with the linear combination of {@code this} and * {@code y}. * * @param a Weight of {@code this}. * @param b Weight of {@code y}. * @param y Vector with which {@code this} is linearly combined. * @return {@code this}, with components equal to * {@code a * this[i] + b * y[i]} for all {@code i}. * @throws DimensionMismatchException if {@code y} is not the same size as * {@code this} vector. */ public RealVector combineToSelf(double a, double b, RealVector y) throws DimensionMismatchException { checkVectorDimensions(y); for (int i = 0; i < getDimension(); i++) { final double xi = getEntry(i); final double yi = y.getEntry(i); setEntry(i, a * xi + b * yi); } return this; } /** * Visits (but does not alter) all entries of this vector in default order * (increasing index). * * @param visitor the visitor to be used to process the entries of this * vector * @return the value returned by {@link RealVectorPreservingVisitor#end()} * at the end of the walk * @since 3.1 */ public double walkInDefaultOrder(final RealVectorPreservingVisitor visitor) { final int dim = getDimension(); visitor.start(dim, 0, dim - 1); for (int i = 0; i < dim; i++) { visitor.visit(i, getEntry(i)); } return visitor.end(); } /** * Visits (but does not alter) some entries

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> of this vector in default order * (increasing index). * * @param visitor visitor to be used to process the entries of this vector * @param start the index of the first entry to be visited * @param end the index of the last entry to be visited (inclusive) * @return the value returned by {@link RealVectorPreservingVisitor#end()} * at the end of the walk * @throws NumberIsTooSmallException if {@code end < start}. * @throws OutOfRangeException if the indices are not valid. * @since 3.1 */ public double walkInDefaultOrder(final RealVectorPreservingVisitor visitor, final int start, final int end) throws NumberIsTooSmallException, OutOfRangeException { checkIndices(start, end); visitor.start(getDimension(), start, end); for (int i = start; i <= end; i++) { visitor.visit(i, getEntry(i)); } return visitor.end(); } /** * Visits (but does not alter) all entries of this vector in optimized * order. The order in which the entries are visited is selected so as to * lead to the most efficient implementation; it might depend on the * concrete implementation of this abstract class. * * @param visitor the visitor to be used to process the entries of this * vector * @return the value returned by {@link RealVectorPreservingVisitor#end()} * at the end of the walk * @since 3.1 */ public double walkInOptimizedOrder(final RealVectorPreservingVisitor visitor) { return walkInDefaultOrder(visitor); } /** * Visits (but does not alter) some entries of this vector in optimized * order. The order in which the entries are visited is selected so as to * lead to the most efficient implementation; it might depend on the * concrete implementation of this abstract class. * * @param visitor visitor to be used to process the entries of this vector * @param start the index of the first entry to be visited * @param end the index of the last entry to be visited (inclusive) * @return the value returned by {@link RealVectorPreservingVisitor#end()} * at the end of the walk * @throws NumberIsTooSmallException if {@code end < start}. * @throws OutOfRangeException if the indices are not valid.

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> * @since 3.1 */ public double walkInOptimizedOrder(final RealVectorPreservingVisitor visitor, final int start, final int end) throws NumberIsTooSmallException, OutOfRangeException { return walkInDefaultOrder(visitor, start, end); } /** * Visits (and possibly alters) all entries of this vector in default order * (increasing index). * * @param visitor the visitor to be used to process and modify the entries * of this vector * @return the value returned by {@link RealVectorChangingVisitor#end()} * at the end of the walk * @since 3.1 */ public double walkInDefaultOrder(final RealVectorChangingVisitor visitor) { final int dim = getDimension(); visitor.start(dim, 0, dim - 1); for (int i = 0; i < dim; i++) { setEntry(i, visitor.visit(i, getEntry(i))); } return visitor.end(); } /** * Visits (and possibly alters) some entries of this vector in default order * (increasing index). * * @param visitor visitor to be used to process the entries of this vector * @param start the index of the first entry to be visited * @param end the index of the last entry to be visited (inclusive) * @return the value returned by {@link RealVectorChangingVisitor#end()} * at the end of the walk * @throws NumberIsTooSmallException if {@code end < start}. * @throws OutOfRangeException if the indices are not valid. * @since 3.1 */ public double walkInDefaultOrder(final RealVectorChangingVisitor visitor, final int start, final int end) throws NumberIsTooSmallException, OutOfRangeException { checkIndices(start, end); visitor.start(getDimension(), start, end); for (int i = start; i <= end; i++) { setEntry(i, visitor.visit(i, getEntry(i))); } return visitor.end(); } /** * Visits (and possibly alters) all entries of this vector in optimized * order. The order in which the entries are visited is selected so as to * lead to the most efficient implementation; it might depend on the * concrete implementation of this abstract class. * * @

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>param visitor the visitor to be used to process the entries of this * vector * @return the value returned by {@link RealVectorChangingVisitor#end()} * at the end of the walk * @since 3.1 */ public double walkInOptimizedOrder(final RealVectorChangingVisitor visitor) { return walkInDefaultOrder(visitor); } /** * Visits (and possibly change) some entries of this vector in optimized * order. The order in which the entries are visited is selected so as to * lead to the most efficient implementation; it might depend on the * concrete implementation of this abstract class. * * @param visitor visitor to be used to process the entries of this vector * @param start the index of the first entry to be visited * @param end the index of the last entry to be visited (inclusive) * @return the value returned by {@link RealVectorChangingVisitor#end()} * at the end of the walk * @throws NumberIsTooSmallException if {@code end < start}. * @throws OutOfRangeException if the indices are not valid. * @since 3.1 */ public double walkInOptimizedOrder(final RealVectorChangingVisitor visitor, final int start, final int end) throws NumberIsTooSmallException, OutOfRangeException { return walkInDefaultOrder(visitor, start, end); } /** An entry in the vector. */ protected class Entry { /** Index of this entry. */ private int index; /** Simple constructor. */ public Entry() { setIndex(0); } /** * Get the value of the entry. * * @return the value of the entry. */ public double getValue() { return getEntry(getIndex()); } /** * Set the value of the entry. * * @param value New value for the entry. */ public void setValue(double value) { setEntry(getIndex(), value); } /** * Get the index of the entry. * * @return the index of the entry. */ public int getIndex() { return index; } /** * Set the index of the entry. * * @param index New index for the entry. */ public void setIndex(int index) { this.index = index; } } /** * <p> * Test for the equality of

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>.ebeDivide(w); } /** {@inheritDoc} */ @Override public double dotProduct(RealVector w) throws DimensionMismatchException { return v.dotProduct(w); } /** {@inheritDoc} */ @Override public double cosine(RealVector w) throws DimensionMismatchException, MathArithmeticException { return v.cosine(w); } /** {@inheritDoc} */ @Override public double getNorm() { return v.getNorm(); } /** {@inheritDoc} */ @Override public double getL1Norm() { return v.getL1Norm(); } /** {@inheritDoc} */ @Override public double getLInfNorm() { return v.getLInfNorm(); } /** {@inheritDoc} */ @Override public double getDistance(RealVector w) throws DimensionMismatchException { return v.getDistance(w); } /** {@inheritDoc} */ @Override public double getL1Distance(RealVector w) throws DimensionMismatchException { return v.getL1Distance(w); } /** {@inheritDoc} */ @Override public double getLInfDistance(RealVector w) throws DimensionMismatchException { return v.getLInfDistance(w); } /** {@inheritDoc} */ @Override public RealVector unitVector() throws MathArithmeticException { return v.unitVector(); } /** * {@inheritDoc} * * @throws MathUnsupportedOperationException in all * circumstances. */ @Override public void unitize() throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** {@inheritDoc} */ @Override public RealMatrix outerProduct(RealVector w) { return v.outerProduct(w); } /** {@inheritDoc} */ @Override public double getEntry(int index) throws OutOfRangeException { return v.getEntry(index); } /** * {@inheritDoc} * * @throws MathUnsupportedOperationException in all * circumstances. */ @Override public void setEntry(int index, double value) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** * {@inheritDoc} * * @throws MathUnsupportedOperationException in all * circumstances. */ @Override public void addToEntry(int index, double value) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); }

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> /** {@inheritDoc} */ @Override public int getDimension() { return v.getDimension(); } /** {@inheritDoc} */ @Override public RealVector append(RealVector w) { return v.append(w); } /** {@inheritDoc} */ @Override public RealVector append(double d) { return v.append(d); } /** {@inheritDoc} */ @Override public RealVector getSubVector(int index, int n) throws OutOfRangeException, NotPositiveException { return v.getSubVector(index, n); } /** * {@inheritDoc} * * @throws MathUnsupportedOperationException in all * circumstances. */ @Override public void setSubVector(int index, RealVector w) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** * {@inheritDoc} * * @throws MathUnsupportedOperationException in all * circumstances. */ @Override public void set(double value) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** {@inheritDoc} */ @Override public double[] toArray() { return v.toArray(); } /** {@inheritDoc} */ @Override public boolean isNaN() { return v.isNaN(); } /** {@inheritDoc} */ @Override public boolean isInfinite() { return v.isInfinite(); } /** {@inheritDoc} */ @Override public RealVector combine(double a, double b, RealVector y) throws DimensionMismatchException { return v.combine(a, b, y); } /** * {@inheritDoc} * * @throws MathUnsupportedOperationException in all * circumstances. */ @Override public RealVector combineToSelf(double a, double b, RealVector y) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** An entry in the vector. */ class UnmodifiableEntry extends Entry { /** {@inheritDoc} */ @Override public double getValue() { return v.getEntry(getIndex()); } /** * {@inheritDoc} * * @throws MathUnsupportedOperationException in all * circumstances. */ @Override public void setValue(double value) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } } }; } }

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> IEEE-754 basic operations and on embedded tables. Almost all operations * are accurate to about 0.5 ulp throughout the domain range. This statement, * of course is only a rough global observed behavior, it is <em>not</em> a * guarantee for <em>every</em> double numbers input (see William Kahan's <a * href="http://en.wikipedia.org/wiki/Rounding#The_table-maker.27s_dilemma">Table * Maker's Dilemma</a>). * </p> * <p> * FastMath additionally implements the following methods not found in Math/StrictMath: * <ul> * <li>{@link #asinh(double)}</li> * <li>{@link #acosh(double)}</li> * <li>{@link #atanh(double)}</li> * </ul> * The following methods are found in Math/StrictMath since 1.6 only, they are provided * by FastMath even in 1.5 Java virtual machines * <ul> * <li>{@link #copySign(double, double)}</li> * <li>{@link #getExponent(double)}</li> * <li>{@link #nextAfter(double,double)}</li> * <li>{@link #nextUp(double)}</li> * <li>{@link #scalb(double, int)}</li> * <li>{@link #copySign(float, float)}</li> * <li>{@link #getExponent(float)}</li> * <li>{@link #nextAfter(float,double)}</li> * <li>{@link #nextUp(float)}</li> * <li>{@link #scalb(float, int)}</li> * </ul> * </p> * @version $Id$ * @since 2.2 */ public class FastMath { /** Archimede's constant PI, ratio of circle circumference to diameter. */ public static final double PI = 105414357.0 / 33554432.0 + 1.984187159361080883e-9; /** Napier's constant e, base of the natural logarithm. */ public static final double E = 2850

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>^53 - double numbers this large must be even. */ private static final double TWO_POWER_53 = 2 * TWO_POWER_52; /** Constant: {@value}. */ private static final double F_1_3 = 1d / 3d; /** Constant: {@value}. */ private static final double F_1_5 = 1d / 5d; /** Constant: {@value}. */ private static final double F_1_7 = 1d / 7d; /** Constant: {@value}. */ private static final double F_1_9 = 1d / 9d; /** Constant: {@value}. */ private static final double F_1_11 = 1d / 11d; /** Constant: {@value}. */ private static final double F_1_13 = 1d / 13d; /** Constant: {@value}. */ private static final double F_1_15 = 1d / 15d; /** Constant: {@value}. */ private static final double F_1_17 = 1d / 17d; /** Constant: {@value}. */ private static final double F_3_4 = 3d / 4d; /** Constant: {@value}. */ private static final double F_15_16 = 15d / 16d; /** Constant: {@value}. */ private static final double F_13_14 = 13d / 14d; /** Constant: {@value}. */ private static final double F_11_12 = 11d / 12d; /** Constant: {@value}. */ private static final double F_9_10 = 9d / 10d; /** Constant: {@value}. */ private static final double F_7_8 = 7d / 8d; /** Constant: {@value}. */ private static final double F_5_6 = 5d / 6d; /** Constant: {@value}. */ private static final double F_1_2 = 1d / 2d; /** Constant: {@value}. */ private static final double F_1_4 = 1d / 4d; /** * Private Constructor */ private FastMath() {} // Generic helper methods

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> /** * Get the high order bits from the mantissa. * Equivalent to adding and subtracting HEX_40000 but also works for very large numbers * * @param d the value to split * @return the high order part of the mantissa */ private static double doubleHighPart(double d) { if (d > -Precision.SAFE_MIN && d < Precision.SAFE_MIN){ return d; // These are un-normalised - don't try to convert } long xl = Double.doubleToLongBits(d); xl = xl & MASK_30BITS; // Drop low order bits return Double.longBitsToDouble(xl); } /** Compute the square root of a number. * <p><b>Note:</b> this implementation currently delegates to {@link Math#sqrt} * @param a number on which evaluation is done * @return square root of a */ public static double sqrt(final double a) { return Math.sqrt(a); } /** Compute the hyperbolic cosine of a number. * @param x number on which evaluation is done * @return hyperbolic cosine of x */ public static double cosh(double x) { if (x != x) { return x; } // cosh[z] = (exp(z) + exp(-z))/2 // for numbers with magnitude 20 or so, // exp(-z) can be ignored in comparison with exp(z) if (x > 20) { if (x >= LOG_MAX_VALUE) { // Avoid overflow (MATH-905). final double t = exp(0.5 * x); return (0.5 * t) * t; } else { return 0.5 * exp(x); } } else if (x < -20) { if (x <= -LOG_MAX_VALUE) { // Avoid overflow (MATH-905). final double t = exp(-0.5 * x); return (0.5 * t) * t; } else { return 0.5 * exp(-x); } } final double hiPrec[] = new double[2]; if (x < 0.0) { x = -x; } exp(x, 0.0

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>aa; // ratio = na/da double ratio = na/da; temp = ratio * HEX_40000000; double ratioa = ratio + temp - temp; double ratiob = ratio - ratioa; // Correct for rounding in division ratiob += (na - daa*ratioa - daa*ratiob - dab*ratioa - dab*ratiob) / da; // Account for nb ratiob += nb / da; // Account for db ratiob += -db * na / da / da; result = ratioa + ratiob; } if (negate) { result = -result; } return result; } /** Compute the inverse hyperbolic cosine of a number. * @param a number on which evaluation is done * @return inverse hyperbolic cosine of a */ public static double acosh(final double a) { return FastMath.log(a + FastMath.sqrt(a * a - 1)); } /** Compute the inverse hyperbolic sine of a number. * @param a number on which evaluation is done * @return inverse hyperbolic sine of a */ public static double asinh(double a) { boolean negative = false; if (a < 0) { negative = true; a = -a; } double absAsinh; if (a > 0.167) { absAsinh = FastMath.log(FastMath.sqrt(a * a + 1) + a); } else { final double a2 = a * a; if (a > 0.097) { absAsinh = a * (1 - a2 * (F_1_3 - a2 * (F_1_5 - a2 * (F_1_7 - a2 * (F_1_9 - a2 * (F_1_11 - a2 * (F_1_13 - a2 * (F_1_15 - a2 * F_1_17 * F_15_16) * F_13_14) * F_11_12) * F_9_10) * F_7_8) * F_5_6) * F_3_4) * F_1_

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>2); } else if (a > 0.036) { absAsinh = a * (1 - a2 * (F_1_3 - a2 * (F_1_5 - a2 * (F_1_7 - a2 * (F_1_9 - a2 * (F_1_11 - a2 * F_1_13 * F_11_12) * F_9_10) * F_7_8) * F_5_6) * F_3_4) * F_1_2); } else if (a > 0.0036) { absAsinh = a * (1 - a2 * (F_1_3 - a2 * (F_1_5 - a2 * (F_1_7 - a2 * F_1_9 * F_7_8) * F_5_6) * F_3_4) * F_1_2); } else { absAsinh = a * (1 - a2 * (F_1_3 - a2 * F_1_5 * F_3_4) * F_1_2); } } return negative ? -absAsinh : absAsinh; } /** Compute the inverse hyperbolic tangent of a number. * @param a number on which evaluation is done * @return inverse hyperbolic tangent of a */ public static double atanh(double a) { boolean negative = false; if (a < 0) { negative = true; a = -a; } double absAtanh; if (a > 0.15) { absAtanh = 0.5 * FastMath.log((1 + a) / (1 - a)); } else { final double a2 = a * a; if (a > 0.087) { absAtanh = a * (1 + a2 * (F_1_3 + a2 * (F_1_5 + a2 * (F_1_7 + a2 * (F_1_9 + a2 * (F_1_11 + a2 * (F_1_13 + a2 * (F_1_15 + a2 * F_1_17)))))))); } else if (a

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>b*xb; /* Subtract from 1 */ ya = -ya; yb = -yb; double za = 1.0 + ya; double zb = -(za - 1.0 - ya); temp = za + yb; zb += -(temp - za - yb); za = temp; /* Square root */ double y; y = sqrt(za); temp = y * HEX_40000000; ya = y + temp - temp; yb = y - ya; /* Extend precision of sqrt */ yb += (za - ya*ya - 2*ya*yb - yb*yb) / (2.0*y); /* Contribution of zb to sqrt */ double dx = zb / (2.0*y); // Compute ratio r = x/y double r = x/y; temp = r * HEX_40000000; double ra = r + temp - temp; double rb = r - ra; rb += (x - ra*ya - ra*yb - rb*ya - rb*yb) / y; // Correct for rounding in division rb += -x * dx / y / y; // Add in effect additional bits of sqrt. temp = ra + rb; rb = -(temp - ra - rb); ra = temp; return atan(ra, rb, false); } /** Compute the arc cosine of a number. * @param x number on which evaluation is done * @return arc cosine of x */ public static double acos(double x) { if (x != x) { return Double.NaN; } if (x > 1.0 || x < -1.0) { return Double.NaN; } if (x == -1.0) { return Math.PI; } if (x == 1.0) { return 0.0; } if (x == 0) { return Math.PI/2.0; } /* Compute acos(x) = atan(sqrt(1-x*x)/x) */ /* Split x */ double temp = x * HEX_40000000; final double xa = x + temp - temp; final double xb = x - xa

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>; /* Square it */ double ya = xa*xa; double yb = xa*xb*2.0 + xb*xb; /* Subtract from 1 */ ya = -ya; yb = -yb; double za = 1.0 + ya; double zb = -(za - 1.0 - ya); temp = za + yb; zb += -(temp - za - yb); za = temp; /* Square root */ double y = sqrt(za); temp = y * HEX_40000000; ya = y + temp - temp; yb = y - ya; /* Extend precision of sqrt */ yb += (za - ya*ya - 2*ya*yb - yb*yb) / (2.0*y); /* Contribution of zb to sqrt */ yb += zb / (2.0*y); y = ya+yb; yb = -(y - ya - yb); // Compute ratio r = y/x double r = y/x; // Did r overflow? if (Double.isInfinite(r)) { // x is effectively zero return Math.PI/2; // so return the appropriate value } double ra = doubleHighPart(r); double rb = r - ra; rb += (y - ra*xa - ra*xb - rb*xa - rb*xb) / x; // Correct for rounding in division rb += yb / x; // Add in effect additional bits of sqrt. temp = ra + rb; rb = -(temp - ra - rb); ra = temp; return atan(ra, rb, x<0); } /** Compute the cubic root of a number. * @param x number on which evaluation is done * @return cubic root of x */ public static double cbrt(double x) { /* Convert input double to bits */ long inbits = Double.doubleToLongBits(x); int exponent = (int) ((inbits >> 52) & 0x7ff) - 1023; boolean subnormal = false; if (exponent == -1023) { if (x == 0) { return x; } /* Subnormal, so normalize */ subnormal = true; x *=

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> * @return sqrt(<i>x</i><sup>2</sup>&nbsp;+<i>y</i><sup>2</sup>) */ public static double hypot(final double x, final double y) { if (Double.isInfinite(x) || Double.isInfinite(y)) { return Double.POSITIVE_INFINITY; } else if (Double.isNaN(x) || Double.isNaN(y)) { return Double.NaN; } else { final int expX = getExponent(x); final int expY = getExponent(y); if (expX > expY + 27) { // y is neglectible with respect to x return abs(x); } else if (expY > expX + 27) { // x is neglectible with respect to y return abs(y); } else { // find an intermediate scale to avoid both overflow and underflow final int middleExp = (expX + expY) / 2; // scale parameters without losing precision final double scaledX = scalb(x, -middleExp); final double scaledY = scalb(y, -middleExp); // compute scaled hypotenuse final double scaledH = sqrt(scaledX * scaledX + scaledY * scaledY); // remove scaling return scalb(scaledH, middleExp); } } } /** * Computes the remainder as prescribed by the IEEE 754 standard. * The remainder value is mathematically equal to {@code x - y*n} * where {@code n} is the mathematical integer closest to the exact mathematical value * of the quotient {@code x/y}. * If two mathematical integers are equally close to {@code x/y} then * {@code n} is the integer that is even. * <p> * <ul> * <li>If either operand is NaN, the result is NaN.</li> * <li>If the result is not NaN, the sign of the result equals the sign of the dividend.</li> * <li>If the dividend is an infinity, or the divisor is a zero, or both, the result is NaN.</li> * <li>If the dividend is finite and the divisor is an infinity, the result equals the dividend.</li> * <li>If the

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> FastMathCalc.printarray(out, "TANGENT_TABLE_A", SINE_TABLE_LEN, TANGENT_TABLE_A); FastMathCalc.printarray(out, "TANGENT_TABLE_B", SINE_TABLE_LEN, TANGENT_TABLE_B); } /** Enclose large data table in nested static class so it's only loaded on first access. */ private static class ExpIntTable { /** Exponential evaluated at integer values, * exp(x) = expIntTableA[x + EXP_INT_TABLE_MAX_INDEX] + expIntTableB[x+EXP_INT_TABLE_MAX_INDEX]. */ private static final double[] EXP_INT_TABLE_A; /** Exponential evaluated at integer values, * exp(x) = expIntTableA[x + EXP_INT_TABLE_MAX_INDEX] + expIntTableB[x+EXP_INT_TABLE_MAX_INDEX] */ private static final double[] EXP_INT_TABLE_B; static { if (RECOMPUTE_TABLES_AT_RUNTIME) { EXP_INT_TABLE_A = new double[FastMath.EXP_INT_TABLE_LEN]; EXP_INT_TABLE_B = new double[FastMath.EXP_INT_TABLE_LEN]; final double tmp[] = new double[2]; final double recip[] = new double[2]; // Populate expIntTable for (int i = 0; i < FastMath.EXP_INT_TABLE_MAX_INDEX; i++) { FastMathCalc.expint(i, tmp); EXP_INT_TABLE_A[i + FastMath.EXP_INT_TABLE_MAX_INDEX] = tmp[0]; EXP_INT_TABLE_B[i + FastMath.EXP_INT_TABLE_MAX_INDEX] = tmp[1]; if (i != 0) { // Negative integer powers FastMathCalc.splitReciprocal(tmp, recip); EXP_INT_TABLE_A[FastMath.EXP_INT_TABLE_MAX_INDEX - i] = recip[0]; EXP_INT_TABLE_B[FastMath.EXP_INT_TABLE_MAX_INDEX - i] = recip[1]; } } } else { EXP_INT_TABLE_A = FastMathLiteral

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>Arrays.loadExpIntA(); EXP_INT_TABLE_B = FastMathLiteralArrays.loadExpIntB(); } } } /** Enclose large data table in nested static class so it's only loaded on first access. */ private static class ExpFracTable { /** Exponential over the range of 0 - 1 in increments of 2^-10 * exp(x/1024) = expFracTableA[x] + expFracTableB[x]. * 1024 = 2^10 */ private static final double[] EXP_FRAC_TABLE_A; /** Exponential over the range of 0 - 1 in increments of 2^-10 * exp(x/1024) = expFracTableA[x] + expFracTableB[x]. */ private static final double[] EXP_FRAC_TABLE_B; static { if (RECOMPUTE_TABLES_AT_RUNTIME) { EXP_FRAC_TABLE_A = new double[FastMath.EXP_FRAC_TABLE_LEN]; EXP_FRAC_TABLE_B = new double[FastMath.EXP_FRAC_TABLE_LEN]; final double tmp[] = new double[2]; // Populate expFracTable final double factor = 1d / (EXP_FRAC_TABLE_LEN - 1); for (int i = 0; i < EXP_FRAC_TABLE_A.length; i++) { FastMathCalc.slowexp(i * factor, tmp); EXP_FRAC_TABLE_A[i] = tmp[0]; EXP_FRAC_TABLE_B[i] = tmp[1]; } } else { EXP_FRAC_TABLE_A = FastMathLiteralArrays.loadExpFracA(); EXP_FRAC_TABLE_B = FastMathLiteralArrays.loadExpFracB(); } } } /** Enclose large data table in nested static class so it's only loaded on first access. */ private static class lnMant { /** Extended precision logarithm table over the range 1 - 2 in increments of 2^-10. */ private static final double[][] LN_MANT; static { if (RECOMPUTE_TABLES_AT_RUNTIME) { LN_MANT = new

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> double[FastMath.LN_MANT_LEN][]; // Populate lnMant table for (int i = 0; i < LN_MANT.length; i++) { final double d = Double.longBitsToDouble( (((long) i) << 42) | 0x3ff0000000000000L ); LN_MANT[i] = FastMathCalc.slowLog(d); } } else { LN_MANT = FastMathLiteralArrays.loadLnMant(); } } } /** Enclose the Cody/Waite reduction (used in "sin", "cos" and "tan"). */ private static class CodyWaite { /** k */ private final int finalK; /** remA */ private final double finalRemA; /** remB */ private final double finalRemB; /** * @param xa Argument. */ CodyWaite(double xa) { // Estimate k. //k = (int)(xa / 1.5707963267948966); int k = (int)(xa * 0.6366197723675814); // Compute remainder. double remA; double remB; while (true) { double a = -k * 1.570796251296997; remA = xa + a; remB = -(remA - xa - a); a = -k * 7.549789948768648E-8; double b = remA; remA = a + b; remB += -(remA - b - a); a = -k * 6.123233995736766E-17; b = remA; remA = a + b; remB += -(remA - b - a); if (remA > 0) { break; } // Remainder is negative, so decrement k and try again. // This should only happen if the input is very close // to an even multiple of pi/2. --k; } this.finalK = k; this.finalRemA

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> double threshold) { super(null); // No custom convergence criterion. this.initialStepBoundFactor = initialStepBoundFactor; this.costRelativeTolerance = costRelativeTolerance; this.parRelativeTolerance = parRelativeTolerance; this.orthoTolerance = orthoTolerance; this.qrRankingThreshold = threshold; } /** {@inheritDoc} */ @Override protected PointVectorValuePair doOptimize() { final int nR = getTarget().length; // Number of observed data. final double[] currentPoint = getStartPoint(); final int nC = currentPoint.length; // Number of parameters. // arrays shared with the other private methods solvedCols = FastMath.min(nR, nC); diagR = new double[nC]; jacNorm = new double[nC]; beta = new double[nC]; permutation = new int[nC]; lmDir = new double[nC]; // local point double delta = 0; double xNorm = 0; double[] diag = new double[nC]; double[] oldX = new double[nC]; double[] oldRes = new double[nR]; double[] oldObj = new double[nR]; double[] qtf = new double[nR]; double[] work1 = new double[nC]; double[] work2 = new double[nC]; double[] work3 = new double[nC]; final RealMatrix weightMatrixSqrt = getWeightSquareRoot(); // Evaluate the function at the starting point and calculate its norm. double[] currentObjective = computeObjectiveValue(currentPoint); double[] currentResiduals = computeResiduals(currentObjective); PointVectorValuePair current = new PointVectorValuePair(currentPoint, currentObjective); double currentCost = computeCost(currentResiduals); // Outer loop. lmPar = 0; boolean firstIteration = true; int iter = 0; final ConvergenceChecker<PointVectorValuePair> checker = getConvergenceChecker(); while (true) { ++iter; final PointVectorValuePair previous = current; // QR decomposition of the jacobian matrix qrDecomposition(computeWeightedJacobian(currentPoint)); weightedResidual = weightMatrixSqrt.operate(currentResiduals); for (int i =

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>0; i < nR; i++) { qtf[i] = weightedResidual[i]; } // compute Qt.res qTy(qtf); // now we don't need Q anymore, // so let jacobian contain the R matrix with its diagonal elements for (int k = 0; k < solvedCols; ++k) { int pk = permutation[k]; weightedJacobian[k][pk] = diagR[pk]; } if (firstIteration) { // scale the point according to the norms of the columns // of the initial jacobian xNorm = 0; for (int k = 0; k < nC; ++k) { double dk = jacNorm[k]; if (dk == 0) { dk = 1.0; } double xk = dk * currentPoint[k]; xNorm += xk * xk; diag[k] = dk; } xNorm = FastMath.sqrt(xNorm); // initialize the step bound delta delta = (xNorm == 0) ? initialStepBoundFactor : (initialStepBoundFactor * xNorm); } // check orthogonality between function vector and jacobian columns double maxCosine = 0; if (currentCost != 0) { for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; double s = jacNorm[pj]; if (s != 0) { double sum = 0; for (int i = 0; i <= j; ++i) { sum += weightedJacobian[i][pj] * qtf[i]; } maxCosine = FastMath.max(maxCosine, FastMath.abs(sum) / (s * currentCost)); } } } if (maxCosine <= orthoTolerance) { // Convergence has been reached. setCost(currentCost); return current; } // rescale if necessary for (int j = 0; j < nC; ++j) { diag[j] = FastMath.max(diag[j], jacNorm[j]); } // Inner loop. for (double ratio = 0; ratio < 1.0e-4;) { // save the state

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; oldX[pj] = currentPoint[pj]; } final double previousCost = currentCost; double[] tmpVec = weightedResidual; weightedResidual = oldRes; oldRes = tmpVec; tmpVec = currentObjective; currentObjective = oldObj; oldObj = tmpVec; // determine the Levenberg-Marquardt parameter determineLMParameter(qtf, delta, diag, work1, work2, work3); // compute the new point and the norm of the evolution direction double lmNorm = 0; for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; lmDir[pj] = -lmDir[pj]; currentPoint[pj] = oldX[pj] + lmDir[pj]; double s = diag[pj] * lmDir[pj]; lmNorm += s * s; } lmNorm = FastMath.sqrt(lmNorm); // on the first iteration, adjust the initial step bound. if (firstIteration) { delta = FastMath.min(delta, lmNorm); } // Evaluate the function at x + p and calculate its norm. currentObjective = computeObjectiveValue(currentPoint); currentResiduals = computeResiduals(currentObjective); current = new PointVectorValuePair(currentPoint, currentObjective); currentCost = computeCost(currentResiduals); // compute the scaled actual reduction double actRed = -1.0; if (0.1 * currentCost < previousCost) { double r = currentCost / previousCost; actRed = 1.0 - r * r; } // compute the scaled predicted reduction // and the scaled directional derivative for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; double dirJ = lmDir[pj]; work1[j] = 0; for (int i = 0; i <= j; ++i) { work1[i] += weightedJacobian[i][pj] * dirJ; } } double coeff1 = 0; for (int j = 0; j < solvedCols; ++j) { coeff1 += work1

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>[j] * work1[j]; } double pc2 = previousCost * previousCost; coeff1 = coeff1 / pc2; double coeff2 = lmPar * lmNorm * lmNorm / pc2; double preRed = coeff1 + 2 * coeff2; double dirDer = -(coeff1 + coeff2); // ratio of the actual to the predicted reduction ratio = (preRed == 0) ? 0 : (actRed / preRed); // update the step bound if (ratio <= 0.25) { double tmp = (actRed < 0) ? (0.5 * dirDer / (dirDer + 0.5 * actRed)) : 0.5; if ((0.1 * currentCost >= previousCost) || (tmp < 0.1)) { tmp = 0.1; } delta = tmp * FastMath.min(delta, 10.0 * lmNorm); lmPar /= tmp; } else if ((lmPar == 0) || (ratio >= 0.75)) { delta = 2 * lmNorm; lmPar *= 0.5; } // test for successful iteration. if (ratio >= 1.0e-4) { // successful iteration, update the norm firstIteration = false; xNorm = 0; for (int k = 0; k < nC; ++k) { double xK = diag[k] * currentPoint[k]; xNorm += xK * xK; } xNorm = FastMath.sqrt(xNorm); // tests for convergence. if (checker != null) { // we use the vectorial convergence checker if (checker.converged(iter, previous, current)) { setCost(currentCost); return current; } } } else { // failed iteration, reset the previous values currentCost = previousCost; for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; currentPoint[pj] = oldX[pj]; } tmpVec = weightedResidual; weightedResidual = oldRes; oldRes = tmpVec; tmpVec = currentObjective; currentObjective = oldObj; oldObj = tmpVec; // Reset "current" to previous values.

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> current = new PointVectorValuePair(currentPoint, currentObjective); } // Default convergence criteria. if ((FastMath.abs(actRed) <= costRelativeTolerance && preRed <= costRelativeTolerance && ratio <= 2.0) || delta <= parRelativeTolerance * xNorm) { setCost(currentCost); return current; } // tests for termination and stringent tolerances // (2.2204e-16 is the machine epsilon for IEEE754) if ((FastMath.abs(actRed) <= 2.2204e-16) && (preRed <= 2.2204e-16) && (ratio <= 2.0)) { throw new ConvergenceException(LocalizedFormats.TOO_SMALL_COST_RELATIVE_TOLERANCE, costRelativeTolerance); } else if (delta <= 2.2204e-16 * xNorm) { throw new ConvergenceException(LocalizedFormats.TOO_SMALL_PARAMETERS_RELATIVE_TOLERANCE, parRelativeTolerance); } else if (maxCosine <= 2.2204e-16) { throw new ConvergenceException(LocalizedFormats.TOO_SMALL_ORTHOGONALITY_TOLERANCE, orthoTolerance); } } } } /** * Determine the Levenberg-Marquardt parameter. * <p>This implementation is a translation in Java of the MINPACK * <a href="http://www.netlib.org/minpack/lmpar.f">lmpar</a> * routine.</p> * <p>This method sets the lmPar and lmDir attributes.</p> * <p>The authors of the original fortran function are:</p> * <ul> * <li>Argonne National Laboratory. MINPACK project. March 1980</li> * <li>Burton S. Garbow</li> * <li>Kenneth E. Hillstrom</li> * <li>Jorge J. More</li> * </ul> * <p>Luc Maisonobe did the Java translation.</p> * * @param qy array containing qTy * @param delta upper bound on the e

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>uclidean norm of diagR * lmDir * @param diag diagonal matrix * @param work1 work array * @param work2 work array * @param work3 work array */ private void determineLMParameter(double[] qy, double delta, double[] diag, double[] work1, double[] work2, double[] work3) { final int nC = weightedJacobian[0].length; // compute and store in x the gauss-newton direction, if the // jacobian is rank-deficient, obtain a least squares solution for (int j = 0; j < rank; ++j) { lmDir[permutation[j]] = qy[j]; } for (int j = rank; j < nC; ++j) { lmDir[permutation[j]] = 0; } for (int k = rank - 1; k >= 0; --k) { int pk = permutation[k]; double ypk = lmDir[pk] / diagR[pk]; for (int i = 0; i < k; ++i) { lmDir[permutation[i]] -= ypk * weightedJacobian[i][pk]; } lmDir[pk] = ypk; } // evaluate the function at the origin, and test // for acceptance of the Gauss-Newton direction double dxNorm = 0; for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; double s = diag[pj] * lmDir[pj]; work1[pj] = s; dxNorm += s * s; } dxNorm = FastMath.sqrt(dxNorm); double fp = dxNorm - delta; if (fp <= 0.1 * delta) { lmPar = 0; return; } // if the jacobian is not rank deficient, the Newton step provides // a lower bound, parl, for the zero of the function, // otherwise set this bound to zero double sum2; double parl = 0; if (rank == solvedCols) { for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; work1[pj] *= diag[pj] / dxNorm; } sum2 = 0; for (int

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> j = 0; j < solvedCols; ++j) { int pj = permutation[j]; double sum = 0; for (int i = 0; i < j; ++i) { sum += weightedJacobian[i][pj] * work1[permutation[i]]; } double s = (work1[pj] - sum) / diagR[pj]; work1[pj] = s; sum2 += s * s; } parl = fp / (delta * sum2); } // calculate an upper bound, paru, for the zero of the function sum2 = 0; for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; double sum = 0; for (int i = 0; i <= j; ++i) { sum += weightedJacobian[i][pj] * qy[i]; } sum /= diag[pj]; sum2 += sum * sum; } double gNorm = FastMath.sqrt(sum2); double paru = gNorm / delta; if (paru == 0) { // 2.2251e-308 is the smallest positive real for IEE754 paru = 2.2251e-308 / FastMath.min(delta, 0.1); } // if the input par lies outside of the interval (parl,paru), // set par to the closer endpoint lmPar = FastMath.min(paru, FastMath.max(lmPar, parl)); if (lmPar == 0) { lmPar = gNorm / dxNorm; } for (int countdown = 10; countdown >= 0; --countdown) { // evaluate the function at the current value of lmPar if (lmPar == 0) { lmPar = FastMath.max(2.2251e-308, 0.001 * paru); } double sPar = FastMath.sqrt(lmPar); for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; work1[pj] = sPar * diag[pj]; } determineLMDirection(qy, work1, work2, work3

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>); dxNorm = 0; for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; double s = diag[pj] * lmDir[pj]; work3[pj] = s; dxNorm += s * s; } dxNorm = FastMath.sqrt(dxNorm); double previousFP = fp; fp = dxNorm - delta; // if the function is small enough, accept the current value // of lmPar, also test for the exceptional cases where parl is zero if ((FastMath.abs(fp) <= 0.1 * delta) || ((parl == 0) && (fp <= previousFP) && (previousFP < 0))) { return; } // compute the Newton correction for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; work1[pj] = work3[pj] * diag[pj] / dxNorm; } for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; work1[pj] /= work2[j]; double tmp = work1[pj]; for (int i = j + 1; i < solvedCols; ++i) { work1[permutation[i]] -= weightedJacobian[i][pj] * tmp; } } sum2 = 0; for (int j = 0; j < solvedCols; ++j) { double s = work1[permutation[j]]; sum2 += s * s; } double correction = fp / (delta * sum2); // depending on the sign of the function, update parl or paru. if (fp > 0) { parl = FastMath.max(parl, lmPar); } else if (fp < 0) { paru = FastMath.min(paru, lmPar); } // compute an improved estimate for lmPar lmPar = FastMath.max(parl, lmPar + correction); } } /** * Solve a*x = b and d*x = 0 in the least squares sense. * <p>This implementation is a translation in Java of the MINPACK * <a href="http://www.netlib.org/minpack/qr

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> Givens rotation which eliminates the // appropriate element in the current row of d if (lmDiag[k] != 0) { final double sin; final double cos; double rkk = weightedJacobian[k][pk]; if (FastMath.abs(rkk) < FastMath.abs(lmDiag[k])) { final double cotan = rkk / lmDiag[k]; sin = 1.0 / FastMath.sqrt(1.0 + cotan * cotan); cos = sin * cotan; } else { final double tan = lmDiag[k] / rkk; cos = 1.0 / FastMath.sqrt(1.0 + tan * tan); sin = cos * tan; } // compute the modified diagonal element of R and // the modified element of (Qty,0) weightedJacobian[k][pk] = cos * rkk + sin * lmDiag[k]; final double temp = cos * work[k] + sin * qtbpj; qtbpj = -sin * work[k] + cos * qtbpj; work[k] = temp; // accumulate the tranformation in the row of s for (int i = k + 1; i < solvedCols; ++i) { double rik = weightedJacobian[i][pk]; final double temp2 = cos * rik + sin * lmDiag[i]; lmDiag[i] = -sin * rik + cos * lmDiag[i]; weightedJacobian[i][pk] = temp2; } } } // store the diagonal element of s and restore // the corresponding diagonal element of R lmDiag[j] = weightedJacobian[j][permutation[j]]; weightedJacobian[j][permutation[j]] = lmDir[j]; } // solve the triangular system for z, if the system is // singular, then obtain a least squares solution int nSing = solvedCols; for (int j = 0; j < solvedCols; ++j) { if ((lmDiag[j] == 0) && (nSing == solvedCols)) { nSing = j; } if (nSing < solvedCols) { work[j] = 0; } } if (nSing > 0) { for (

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> the current point. * @exception ConvergenceException if the decomposition cannot be performed */ private void qrDecomposition(RealMatrix jacobian) throws ConvergenceException { // Code in this class assumes that the weighted Jacobian is -(W^(1/2) J), // hence the multiplication by -1. weightedJacobian = jacobian.scalarMultiply(-1).getData(); final int nR = weightedJacobian.length; final int nC = weightedJacobian[0].length; // initializations for (int k = 0; k < nC; ++k) { permutation[k] = k; double norm2 = 0; for (int i = 0; i < nR; ++i) { double akk = weightedJacobian[i][k]; norm2 += akk * akk; } jacNorm[k] = FastMath.sqrt(norm2); } // transform the matrix column after column for (int k = 0; k < nC; ++k) { // select the column with the greatest norm on active components int nextColumn = -1; double ak2 = Double.NEGATIVE_INFINITY; for (int i = k; i < nC; ++i) { double norm2 = 0; for (int j = k; j < nR; ++j) { double aki = weightedJacobian[j][permutation[i]]; norm2 += aki * aki; } if (Double.isInfinite(norm2) || Double.isNaN(norm2)) { throw new ConvergenceException(LocalizedFormats.UNABLE_TO_PERFORM_QR_DECOMPOSITION_ON_JACOBIAN, nR, nC); } if (norm2 > ak2) { nextColumn = i; ak2 = norm2; } } if (ak2 <= qrRankingThreshold) { rank = k; return; } int pk = permutation[nextColumn]; permutation[nextColumn] = permutation[k]; permutation[k] = pk; // choose alpha such that Hk.u = alpha ek double akk = weightedJacobian[k][pk]; double alpha = (akk > 0) ? -FastMath.sqrt(ak2) : FastMath.sqrt(ak2); double bet

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.math3.linear; import java.io.Serializable; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.MathUnsupportedOperationException; /** * Implementation of a diagonal matrix. * <br/> * Caveat: This implementation is minimal; it is currently solely aimed * at solving issue MATH-924. In particular many methods just throw * {@code MathUnsupportedOperationException}. * * @version $Id$ */ public class DiagonalMatrix extends AbstractRealMatrix implements Serializable { /** Serializable version identifier. */ private static final long serialVersionUID = 20121229L; /** Entries of the diagonal. */ private double[] data; /** * Creates a matrix with no data. */ public DiagonalMatrix() {} /** * Creates a matrix with the supplied dimension. * * @param dimension Number of rows and columns in the new matrix. * @param columnDimension Number of columns in the new matrix. * @throws NotStrictlyPositiveException if the dimension is * not positive. */ public DiagonalMatrix(final int dimension) throws NotStrictlyPositiveException { super(dimension, dimension); data = new double[dimension]; } /** * Creates a matrix using the input array as the underlying data. * <br/> * The input

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> array is copied, not referenced. * * @param d Data for the new matrix. */ public DiagonalMatrix(final double[] d) { data = d.clone(); } /** * Creates a matrix using the input array as the underlying data. * <br/> * If an array is built specially in order to be embedded in a * RealMatrix and not used directly, the {@code copyArray} may be * set to {@code false}. This will prevent the copying and improve * performance as no new array will be built and no data will be copied. * * @param d Data for new matrix. * @param copyArray if {@code true}, the input array will be copied, * otherwise it will be referenced. */ public DiagonalMatrix(final double[] d, final boolean copyArray) { data = copyArray ? d.clone() : d; } /** * {@inheritDoc} * * @throws DimensionMismatchException if the requested dimensions are not equal. */ @Override public RealMatrix createMatrix(final int rowDimension, final int columnDimension) throws NotStrictlyPositiveException, DimensionMismatchException { if (rowDimension != columnDimension) { throw new DimensionMismatchException(rowDimension, columnDimension); } return new DiagonalMatrix(rowDimension); } /** {@inheritDoc} */ @Override public RealMatrix copy() { return new DiagonalMatrix(data); } /** * Compute the sum of {@code this} and {@code m}. * * @param m Matrix to be added. * @return {@code this + m}. * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this}. */ public DiagonalMatrix add(final DiagonalMatrix m) throws MatrixDimensionMismatchException { // Safety check. MatrixUtils.checkAdditionCompatible(this, m); final int dim = getRowDimension(); final double[] outData = new double[dim]; for (int i = 0; i < dim; i++) { outData[i] = data[i] + m.data[i]; } return new DiagonalMatrix(outData, false); } /** * Returns {@code this} minus {@code m}. * * @param m Matrix to be subtracted. * @return {@code this -

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> m} * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this}. */ public DiagonalMatrix subtract(final DiagonalMatrix m) throws MatrixDimensionMismatchException { MatrixUtils.checkSubtractionCompatible(this, m); final int dim = getRowDimension(); final double[] outData = new double[dim]; for (int i = 0; i < dim; i++) { outData[i] = data[i] - m.data[i]; } return new DiagonalMatrix(outData, false); } /** * Returns the result of postmultiplying {@code this} by {@code m}. * * @param m matrix to postmultiply by * @return {@code this * m} * @throws DimensionMismatchException if * {@code columnDimension(this) != rowDimension(m)} */ public DiagonalMatrix multiply(final DiagonalMatrix m) throws DimensionMismatchException { MatrixUtils.checkMultiplicationCompatible(this, m); final int dim = getRowDimension(); final double[] outData = new double[dim]; for (int i = 0; i < dim; i++) { outData[i] = data[i] * m.data[i]; } return new DiagonalMatrix(outData, false); } /** * Returns the result of postmultiplying {@code this} by {@code m}. * * @param m matrix to postmultiply by * @return {@code this * m} * @throws DimensionMismatchException if * {@code columnDimension(this) != rowDimension(m)} */ public RealMatrix multiply(final RealMatrix m) throws DimensionMismatchException { if (m instanceof DiagonalMatrix) { return multiply((DiagonalMatrix) m); } else { MatrixUtils.checkMultiplicationCompatible(this, m); final int nRows = m.getRowDimension(); final int nCols = m.getColumnDimension(); final double[][] product = new double[nRows][nCols]; for (int r = 0; r < nRows; r++) { for (int c = 0; c < nCols; c++) { product[r][c] = data[r] * m.getEntry(r, c); } } return new Array2DRowRealMatrix(product, false);

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> } } /** {@inheritDoc} */ @Override public double[][] getData() { final int dim = getRowDimension(); final double[][] out = new double[dim][dim]; for (int i = 0; i < dim; i++) { out[i][i] = data[i]; } return out; } /** * Gets a reference to the underlying data array. * * @return 1-dimensional array of entries. */ public double[] getDataRef() { return data; } /** * @throws MathUnsupportedOperationException */ @Override public void setSubMatrix(final double[][] subMatrix, final int row, final int column) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** {@inheritDoc} */ @Override public double getEntry(final int row, final int column) throws OutOfRangeException { MatrixUtils.checkMatrixIndex(this, row, column); return row == column ? data[row] : 0; } /** {@inheritDoc} * @throws MathUnsupportedOperationException if {@code row != column}. */ @Override public void setEntry(final int row, final int column, final double value) throws OutOfRangeException, MathUnsupportedOperationException { if (row != column) { throw new MathUnsupportedOperationException(); } MatrixUtils.checkMatrixIndex(this, row, column); data[row] = value; } /** {@inheritDoc} * @throws MathUnsupportedOperationException if {@code row != column}. */ @Override public void addToEntry(final int row, final int column, final double increment) throws OutOfRangeException, MathUnsupportedOperationException { if (row != column) { throw new MathUnsupportedOperationException(); } MatrixUtils.checkMatrixIndex(this, row, column); data[row] += increment; } /** {@inheritDoc} * @throws MathUnsupportedOperationException if {@code row != column}. */ @Override public void multiplyEntry(final int row, final int column, final double factor) throws OutOfRangeException, MathUnsupportedOperationException { if (row != column) { throw new MathUnsupportedOperationException(); } MatrixUtils.checkMatrixIndex(this, row, column); data[row] *= factor; } /** {@inheritDoc} */ @Override public int getRow

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>Dimension() { return data == null ? 0 : data.length; } /** {@inheritDoc} */ @Override public int getColumnDimension() { return getRowDimension(); } /** {@inheritDoc} */ @Override public double[] operate(final double[] v) throws DimensionMismatchException { return multiply(new DiagonalMatrix(v, false)).getDataRef(); } /** {@inheritDoc} */ @Override public double[] preMultiply(final double[] v) throws DimensionMismatchException { return operate(v); } /** {@inheritDoc} */ @Override public double walkInRowOrder(final RealMatrixChangingVisitor visitor) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** {@inheritDoc} */ @Override public double walkInRowOrder(final RealMatrixPreservingVisitor visitor) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** {@inheritDoc} */ @Override public double walkInRowOrder(final RealMatrixChangingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** {@inheritDoc} */ @Override public double walkInRowOrder(final RealMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** {@inheritDoc} */ @Override public double walkInColumnOrder(final RealMatrixChangingVisitor visitor) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** {@inheritDoc} */ @Override public double walkInColumnOrder(final RealMatrixPreservingVisitor visitor) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** {@inheritDoc} */ @Override public double walkInColumnOrder(final RealMatrixChangingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** {@inheritDoc} */ @Override public double walkInColumnOrder(final RealMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } }

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>84252L); final LevenbergMarquardtOptimizer optim = new LevenbergMarquardtOptimizer(); final PolynomialFitter fitter = new PolynomialFitter(optim); final double[] coeff = { 12.9, -3.4, 2.1 }; // 12.9 - 3.4 x + 2.1 x^2 final PolynomialFunction f = new PolynomialFunction(coeff); // Collect data from a known polynomial. for (int i = 0; i < 100; i++) { final double x = rng.sample(); fitter.addObservedPoint(x, f.value(x)); } // Start fit from initial guesses that are far from the optimal values. final double[] best = fitter.fit(new double[] { -1e-20, 3e15, -5e25 }); TestUtils.assertEquals("best != coeff", coeff, best, 1e-12); } @Test public void testNoError() { Random randomizer = new Random(64925784252l); for (int degree = 1; degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(new LevenbergMarquardtOptimizer()); for (int i = 0; i <= degree; ++i) { fitter.addObservedPoint(1.0, i, p.value(i)); } final double[] init = new double[degree + 1]; PolynomialFunction fitted = new PolynomialFunction(fitter.fit(init)); for (double x = -1.0; x < 1.0; x += 0.01) { double error = FastMath.abs(p.value(x) - fitted.value(x)) / (1.0 + FastMath.abs(p.value(x))); Assert.assertEquals(0.0, error, 1.0e-6); } } } @Test public void testSmallError() { Random randomizer = new Random(53882150042l); double maxError = 0; for (int degree = 0;

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(new LevenbergMarquardtOptimizer()); for (double x = -1.0; x < 1.0; x += 0.01) { fitter.addObservedPoint(1.0, x, p.value(x) + 0.1 * randomizer.nextGaussian()); } final double[] init = new double[degree + 1]; PolynomialFunction fitted = new PolynomialFunction(fitter.fit(init)); for (double x = -1.0; x < 1.0; x += 0.01) { double error = FastMath.abs(p.value(x) - fitted.value(x)) / (1.0 + FastMath.abs(p.value(x))); maxError = FastMath.max(maxError, error); Assert.assertTrue(FastMath.abs(error) < 0.1); } } Assert.assertTrue(maxError > 0.01); } @Test public void testMath798() { final double tol = 1e-14; final SimpleVectorValueChecker checker = new SimpleVectorValueChecker(tol, tol); final double[] init = new double[] { 0, 0 }; final int maxEval = 3; final double[] lm = doMath798(new LevenbergMarquardtOptimizer(checker), maxEval, init); final double[] gn = doMath798(new GaussNewtonOptimizer(checker), maxEval, init); for (int i = 0; i <= 1; i++) { Assert.assertEquals(lm[i], gn[i], tol); } } /** * This test shows that the user can set the maximum number of iterations * to avoid running for too long. * But in the test case, the real problem is that the tolerance is way too * stringent. */ @Test(expected=TooManyEvaluationsException.class) public void testMath798WithToleranceTooLow() { final double tol = 1e-100; final SimpleVectorValueChecker checker = new SimpleVectorValueChecker(tol, tol);

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>.fit(init)); for (double x = -1.0; x < 1.0; x += 0.01) { double error = FastMath.abs(p.value(x) - fitted.value(x)) / (1.0 + FastMath.abs(p.value(x))); maxError = FastMath.max(maxError, error); Assert.assertTrue(FastMath.abs(error) < 0.01); } } Assert.assertTrue(maxError > 0.001); } private void checkUnsolvableProblem(MultivariateVectorOptimizer optimizer, boolean solvable) { Random randomizer = new Random(1248788532l); for (int degree = 0; degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(optimizer); // reusing the same point over and over again does not bring // information, the problem cannot be solved in this case for // degrees greater than 1 (but one point is sufficient for // degree 0) for (double x = -1.0; x < 1.0; x += 0.01) { fitter.addObservedPoint(1.0, 0.0, p.value(0.0)); } try { final double[] init = new double[degree + 1]; fitter.fit(init); Assert.assertTrue(solvable || (degree == 0)); } catch(ConvergenceException e) { Assert.assertTrue((! solvable) && (degree > 0)); } } } private PolynomialFunction buildRandomPolynomial(int degree, Random randomizer) { final double[] coefficients = new double[degree + 1]; for (int i = 0; i <= degree; ++i) { coefficients[i] = randomizer.nextGaussian(); } return new PolynomialFunction(coefficients); } }

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> #BlockRealMatrix(int, int, double[][], boolean)} constructor. * </p> * @param rawData Data array in raw layout. * @return a new data array containing the same entries but in blocks layout. * @throws DimensionMismatchException if {@code rawData} is not rectangular. * @see #createBlocksLayout(int, int) * @see #BlockRealMatrix(int, int, double[][], boolean) */ public static double[][] toBlocksLayout(final double[][] rawData) throws DimensionMismatchException { final int rows = rawData.length; final int columns = rawData[0].length; final int blockRows = (rows + BLOCK_SIZE - 1) / BLOCK_SIZE; final int blockColumns = (columns + BLOCK_SIZE - 1) / BLOCK_SIZE; // safety checks for (int i = 0; i < rawData.length; ++i) { final int length = rawData[i].length; if (length != columns) { throw new DimensionMismatchException(columns, length); } } // convert array final double[][] blocks = new double[blockRows * blockColumns][]; int blockIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); final int iHeight = pEnd - pStart; for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final int jWidth = qEnd - qStart; // allocate new block final double[] block = new double[iHeight * jWidth]; blocks[blockIndex] = block; // copy data int index = 0; for (int p = pStart; p < pEnd; ++p) { System.arraycopy(rawData[p], qStart, block, index, jWidth); index += jWidth; } ++blockIndex; } } return blocks; } /** * Create a data array in blocks layout. * <p> * This method can be used to create the array

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> argument of the {@link * #BlockRealMatrix(int, int, double[][], boolean)} constructor. * </p> * @param rows Number of rows in the new matrix. * @param columns Number of columns in the new matrix. * @return a new data array in blocks layout. * @see #toBlocksLayout(double[][]) * @see #BlockRealMatrix(int, int, double[][], boolean) */ public static double[][] createBlocksLayout(final int rows, final int columns) { final int blockRows = (rows + BLOCK_SIZE - 1) / BLOCK_SIZE; final int blockColumns = (columns + BLOCK_SIZE - 1) / BLOCK_SIZE; final double[][] blocks = new double[blockRows * blockColumns][]; int blockIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); final int iHeight = pEnd - pStart; for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final int jWidth = qEnd - qStart; blocks[blockIndex] = new double[iHeight * jWidth]; ++blockIndex; } } return blocks; } /** {@inheritDoc} */ @Override public BlockRealMatrix createMatrix(final int rowDimension, final int columnDimension) throws NotStrictlyPositiveException { return new BlockRealMatrix(rowDimension, columnDimension); } /** {@inheritDoc} */ @Override public BlockRealMatrix copy() { // create an empty matrix BlockRealMatrix copied = new BlockRealMatrix(rows, columns); // copy the blocks for (int i = 0; i < blocks.length; ++i) { System.arraycopy(blocks[i], 0, copied.blocks[i], 0, blocks[i].length); } return copied; } /** {@inheritDoc} */ @Override public BlockRealMatrix add(final RealMatrix m) throws MatrixDimensionMismatchException { try { return add((BlockRealMatrix) m);

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> } catch (ClassCastException cce) { // safety check MatrixUtils.checkAdditionCompatible(this, m); final BlockRealMatrix out = new BlockRealMatrix(rows, columns); // perform addition block-wise, to ensure good cache behavior int blockIndex = 0; for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) { for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) { // perform addition on the current block final double[] outBlock = out.blocks[blockIndex]; final double[] tBlock = blocks[blockIndex]; final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); int k = 0; for (int p = pStart; p < pEnd; ++p) { for (int q = qStart; q < qEnd; ++q) { outBlock[k] = tBlock[k] + m.getEntry(p, q); ++k; } } // go to next block ++blockIndex; } } return out; } } /** * Compute the sum of this matrix and {@code m}. * * @param m Matrix to be added. * @return {@code this} + m. * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as this matrix. */ public BlockRealMatrix add(final BlockRealMatrix m) throws MatrixDimensionMismatchException { // safety check MatrixUtils.checkAdditionCompatible(this, m); final BlockRealMatrix out = new BlockRealMatrix(rows, columns); // perform addition block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < out.blocks.length; ++blockIndex) { final double[] outBlock = out.blocks[blockIndex]; final double[] tBlock = blocks[blockIndex]; final double[] mBlock = m.blocks[blockIndex]; for (int k = 0; k < outBlock.length; ++k) { outBlock[k] = tBlock[k] + mBlock[k

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>]; } } return out; } /** {@inheritDoc} */ @Override public BlockRealMatrix subtract(final RealMatrix m) throws MatrixDimensionMismatchException { try { return subtract((BlockRealMatrix) m); } catch (ClassCastException cce) { // safety check MatrixUtils.checkSubtractionCompatible(this, m); final BlockRealMatrix out = new BlockRealMatrix(rows, columns); // perform subtraction block-wise, to ensure good cache behavior int blockIndex = 0; for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) { for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) { // perform subtraction on the current block final double[] outBlock = out.blocks[blockIndex]; final double[] tBlock = blocks[blockIndex]; final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); int k = 0; for (int p = pStart; p < pEnd; ++p) { for (int q = qStart; q < qEnd; ++q) { outBlock[k] = tBlock[k] - m.getEntry(p, q); ++k; } } // go to next block ++blockIndex; } } return out; } } /** * Subtract {@code m} from this matrix. * * @param m Matrix to be subtracted. * @return {@code this} - m. * @throws MatrixDimensionMismatchException if {@code m} is not the * same size as this matrix. */ public BlockRealMatrix subtract(final BlockRealMatrix m) throws MatrixDimensionMismatchException { // safety check MatrixUtils.checkSubtractionCompatible(this, m); final BlockRealMatrix out = new BlockRealMatrix(rows, columns); // perform subtraction block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < out.blocks.length; ++blockIndex) { final double[] outBlock = out.blocks[blockIndex]; final double[] tBlock = blocks

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>[blockIndex]; final double[] mBlock = m.blocks[blockIndex]; for (int k = 0; k < outBlock.length; ++k) { outBlock[k] = tBlock[k] - mBlock[k]; } } return out; } /** {@inheritDoc} */ @Override public BlockRealMatrix scalarAdd(final double d) { final BlockRealMatrix out = new BlockRealMatrix(rows, columns); // perform subtraction block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < out.blocks.length; ++blockIndex) { final double[] outBlock = out.blocks[blockIndex]; final double[] tBlock = blocks[blockIndex]; for (int k = 0; k < outBlock.length; ++k) { outBlock[k] = tBlock[k] + d; } } return out; } /** {@inheritDoc} */ @Override public RealMatrix scalarMultiply(final double d) { final BlockRealMatrix out = new BlockRealMatrix(rows, columns); // perform subtraction block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < out.blocks.length; ++blockIndex) { final double[] outBlock = out.blocks[blockIndex]; final double[] tBlock = blocks[blockIndex]; for (int k = 0; k < outBlock.length; ++k) { outBlock[k] = tBlock[k] * d; } } return out; } /** {@inheritDoc} */ @Override public BlockRealMatrix multiply(final RealMatrix m) throws DimensionMismatchException { try { return multiply((BlockRealMatrix) m); } catch (ClassCastException cce) { // safety check MatrixUtils.checkMultiplicationCompatible(this, m); final BlockRealMatrix out = new BlockRealMatrix(rows, m.getColumnDimension()); // perform multiplication block-wise, to ensure good cache behavior int blockIndex = 0; for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int jBlock = 0; jBlock <

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> out.blockColumns; ++jBlock) { final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, m.getColumnDimension()); // select current block final double[] outBlock = out.blocks[blockIndex]; // perform multiplication on current block for (int kBlock = 0; kBlock < blockColumns; ++kBlock) { final int kWidth = blockWidth(kBlock); final double[] tBlock = blocks[iBlock * blockColumns + kBlock]; final int rStart = kBlock * BLOCK_SIZE; int k = 0; for (int p = pStart; p < pEnd; ++p) { final int lStart = (p - pStart) * kWidth; final int lEnd = lStart + kWidth; for (int q = qStart; q < qEnd; ++q) { double sum = 0; int r = rStart; for (int l = lStart; l < lEnd; ++l) { sum += tBlock[l] * m.getEntry(r, q); ++r; } outBlock[k] += sum; ++k; } } } // go to next block ++blockIndex; } } return out; } } /** * Returns the result of postmultiplying this by {@code m}. * * @param m Matrix to postmultiply by. * @return {@code this} * m. * @throws DimensionMismatchException if the matrices are not compatible. */ public BlockRealMatrix multiply(BlockRealMatrix m) throws DimensionMismatchException { // safety check MatrixUtils.checkMultiplicationCompatible(this, m); final BlockRealMatrix out = new BlockRealMatrix(rows, m.columns); // perform multiplication block-wise, to ensure good cache behavior int blockIndex = 0; for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) { final int jWidth = out.blockWidth(jBlock); final int jWidth2 =

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> jWidth + jWidth; final int jWidth3 = jWidth2 + jWidth; final int jWidth4 = jWidth3 + jWidth; // select current block final double[] outBlock = out.blocks[blockIndex]; // perform multiplication on current block for (int kBlock = 0; kBlock < blockColumns; ++kBlock) { final int kWidth = blockWidth(kBlock); final double[] tBlock = blocks[iBlock * blockColumns + kBlock]; final double[] mBlock = m.blocks[kBlock * m.blockColumns + jBlock]; int k = 0; for (int p = pStart; p < pEnd; ++p) { final int lStart = (p - pStart) * kWidth; final int lEnd = lStart + kWidth; for (int nStart = 0; nStart < jWidth; ++nStart) { double sum = 0; int l = lStart; int n = nStart; while (l < lEnd - 3) { sum += tBlock[l] * mBlock[n] + tBlock[l + 1] * mBlock[n + jWidth] + tBlock[l + 2] * mBlock[n + jWidth2] + tBlock[l + 3] * mBlock[n + jWidth3]; l += 4; n += jWidth4; } while (l < lEnd) { sum += tBlock[l++] * mBlock[n]; n += jWidth; } outBlock[k] += sum; ++k; } } } // go to next block ++blockIndex; } } return out; } /** {@inheritDoc} */ @Override public double[][] getData() { final double[][] data = new double[getRowDimension()][getColumnDimension()]; final int lastColumns = columns - (blockColumns - 1) * BLOCK_SIZE; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); int regularPos = 0; int lastPos = 0; for (int p = pStart; p < pEnd

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>; ++p) { final double[] dataP = data[p]; int blockIndex = iBlock * blockColumns; int dataPos = 0; for (int jBlock = 0; jBlock < blockColumns - 1; ++jBlock) { System.arraycopy(blocks[blockIndex++], regularPos, dataP, dataPos, BLOCK_SIZE); dataPos += BLOCK_SIZE; } System.arraycopy(blocks[blockIndex], lastPos, dataP, dataPos, lastColumns); regularPos += BLOCK_SIZE; lastPos += lastColumns; } } return data; } /** {@inheritDoc} */ @Override public double getNorm() { final double[] colSums = new double[BLOCK_SIZE]; double maxColSum = 0; for (int jBlock = 0; jBlock < blockColumns; jBlock++) { final int jWidth = blockWidth(jBlock); Arrays.fill(colSums, 0, jWidth, 0.0); for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int iHeight = blockHeight(iBlock); final double[] block = blocks[iBlock * blockColumns + jBlock]; for (int j = 0; j < jWidth; ++j) { double sum = 0; for (int i = 0; i < iHeight; ++i) { sum += FastMath.abs(block[i * jWidth + j]); } colSums[j] += sum; } } for (int j = 0; j < jWidth; ++j) { maxColSum = FastMath.max(maxColSum, colSums[j]); } } return maxColSum; } /** {@inheritDoc} */ @Override public double getFrobeniusNorm() { double sum2 = 0; for (int blockIndex = 0; blockIndex < blocks.length; ++blockIndex) { for (final double entry : blocks[blockIndex]) { sum2 += entry * entry; } } return FastMath.sqrt(sum2); } /** {@inheritDoc} */ @Override public BlockRealMatrix getSubMatrix(final int startRow, final int endRow, final int startColumn, final int endColumn

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>; ++iBlock) { final int iHeight = blockHeight(iBlock); final int firstRow = iBlock * BLOCK_SIZE; final int iStart = FastMath.max(row, firstRow); final int iEnd = FastMath.min(endRow + 1, firstRow + iHeight); for (int jBlock = blockStartColumn; jBlock < blockEndColumn; ++jBlock) { final int jWidth = blockWidth(jBlock); final int firstColumn = jBlock * BLOCK_SIZE; final int jStart = FastMath.max(column, firstColumn); final int jEnd = FastMath.min(endColumn + 1, firstColumn + jWidth); final int jLength = jEnd - jStart; // handle one block, row by row final double[] block = blocks[iBlock * blockColumns + jBlock]; for (int i = iStart; i < iEnd; ++i) { System.arraycopy(subMatrix[i - row], jStart - column, block, (i - firstRow) * jWidth + (jStart - firstColumn), jLength); } } } } /** {@inheritDoc} */ @Override public BlockRealMatrix getRowMatrix(final int row) throws OutOfRangeException { MatrixUtils.checkRowIndex(this, row); final BlockRealMatrix out = new BlockRealMatrix(1, columns); // perform copy block-wise, to ensure good cache behavior final int iBlock = row / BLOCK_SIZE; final int iRow = row - iBlock * BLOCK_SIZE; int outBlockIndex = 0; int outIndex = 0; double[] outBlock = out.blocks[outBlockIndex]; for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final double[] block = blocks[iBlock * blockColumns + jBlock]; final int available = outBlock.length - outIndex; if (jWidth > available) { System.arraycopy(block, iRow * jWidth, outBlock, outIndex, available); outBlock = out.blocks[++outBlockIndex]; System.arraycopy(block, iRow * jWidth, outBlock, 0, jWidth - available); outIndex = jWidth - available; } else { System

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int iHeight = blockHeight(iBlock); final double[] block = blocks[iBlock * blockColumns + jBlock]; for (int i = 0; i < iHeight; ++i) { block[i * jWidth + jColumn] = array[outIndex++]; } } } /** {@inheritDoc} */ @Override public double getEntry(final int row, final int column) throws OutOfRangeException { MatrixUtils.checkMatrixIndex(this, row, column); final int iBlock = row / BLOCK_SIZE; final int jBlock = column / BLOCK_SIZE; final int k = (row - iBlock * BLOCK_SIZE) * blockWidth(jBlock) + (column - jBlock * BLOCK_SIZE); return blocks[iBlock * blockColumns + jBlock][k]; } /** {@inheritDoc} */ @Override public void setEntry(final int row, final int column, final double value) throws OutOfRangeException { MatrixUtils.checkMatrixIndex(this, row, column); final int iBlock = row / BLOCK_SIZE; final int jBlock = column / BLOCK_SIZE; final int k = (row - iBlock * BLOCK_SIZE) * blockWidth(jBlock) + (column - jBlock * BLOCK_SIZE); blocks[iBlock * blockColumns + jBlock][k] = value; } /** {@inheritDoc} */ @Override public void addToEntry(final int row, final int column, final double increment) throws OutOfRangeException { MatrixUtils.checkMatrixIndex(this, row, column); final int iBlock = row / BLOCK_SIZE; final int jBlock = column / BLOCK_SIZE; final int k = (row - iBlock * BLOCK_SIZE) * blockWidth(jBlock) + (column - jBlock * BLOCK_SIZE); blocks[iBlock * blockColumns + jBlock][k] += increment; } /** {@inheritDoc} */ @Override public void multiplyEntry(final int row, final int column, final double factor) throws OutOfRangeException { MatrixUtils.checkMatrixIndex(this, row, column); final int iBlock = row / BLOCK_SIZE; final int jBlock = column / BLOCK_

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>SIZE; final int k = (row - iBlock * BLOCK_SIZE) * blockWidth(jBlock) + (column - jBlock * BLOCK_SIZE); blocks[iBlock * blockColumns + jBlock][k] *= factor; } /** {@inheritDoc} */ @Override public BlockRealMatrix transpose() { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); final BlockRealMatrix out = new BlockRealMatrix(nCols, nRows); // perform transpose block-wise, to ensure good cache behavior int blockIndex = 0; for (int iBlock = 0; iBlock < blockColumns; ++iBlock) { for (int jBlock = 0; jBlock < blockRows; ++jBlock) { // transpose current block final double[] outBlock = out.blocks[blockIndex]; final double[] tBlock = blocks[jBlock * blockColumns + iBlock]; final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, columns); final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, rows); int k = 0; for (int p = pStart; p < pEnd; ++p) { final int lInc = pEnd - pStart; int l = p - pStart; for (int q = qStart; q < qEnd; ++q) { outBlock[k] = tBlock[l]; ++k; l+= lInc; } } // go to next block ++blockIndex; } } return out; } /** {@inheritDoc} */ @Override public int getRowDimension() { return rows; } /** {@inheritDoc} */ @Override public int getColumnDimension() { return columns; } /** {@inheritDoc} */ @Override public double[] operate(final double[] v) throws DimensionMismatchException { if (v.length != columns) { throw new DimensionMismatchException(v.length, columns); } final double[] out = new double[rows]; // perform multiplication block-wise, to ensure good cache behavior for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart =

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final double[] block = blocks[iBlock * blockColumns + jBlock]; final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); int k = 0; for (int p = pStart; p < pEnd; ++p) { double sum = 0; int q = qStart; while (q < qEnd - 3) { sum += block[k] * v[q] + block[k + 1] * v[q + 1] + block[k + 2] * v[q + 2] + block[k + 3] * v[q + 3]; k += 4; q += 4; } while (q < qEnd) { sum += block[k++] * v[q++]; } out[p] += sum; } } } return out; } /** {@inheritDoc} */ @Override public double[] preMultiply(final double[] v) throws DimensionMismatchException { if (v.length != rows) { throw new DimensionMismatchException(v.length, rows); } final double[] out = new double[columns]; // perform multiplication block-wise, to ensure good cache behavior for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final int jWidth2 = jWidth + jWidth; final int jWidth3 = jWidth2 + jWidth; final int jWidth4 = jWidth3 + jWidth; final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final double[] block = blocks[iBlock * blockColumns + jBlock]; final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int q =

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> qStart; q < qEnd; ++q) { int k = q - qStart; double sum = 0; int p = pStart; while (p < pEnd - 3) { sum += block[k] * v[p] + block[k + jWidth] * v[p + 1] + block[k + jWidth2] * v[p + 2] + block[k + jWidth3] * v[p + 3]; k += jWidth4; p += 4; } while (p < pEnd) { sum += block[k] * v[p++]; k += jWidth; } out[q] += sum; } } } return out; } /** {@inheritDoc} */ @Override public double walkInRowOrder(final RealMatrixChangingVisitor visitor) { visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int p = pStart; p < pEnd; ++p) { for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final double[] block = blocks[iBlock * blockColumns + jBlock]; int k = (p - pStart) * jWidth; for (int q = qStart; q < qEnd; ++q) { block[k] = visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); } /** {@inheritDoc} */ @Override public double walkInRowOrder(final RealMatrixPreservingVisitor visitor) { visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock *

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int p = pStart; p < pEnd; ++p) { for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final double[] block = blocks[iBlock * blockColumns + jBlock]; int k = (p - pStart) * jWidth; for (int q = qStart; q < qEnd; ++q) { visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); } /** {@inheritDoc} */ @Override public double walkInRowOrder(final RealMatrixChangingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(rows, columns, startRow, endRow, startColumn, endColumn); for (int iBlock = startRow / BLOCK_SIZE; iBlock < 1 + endRow / BLOCK_SIZE; ++iBlock) { final int p0 = iBlock * BLOCK_SIZE; final int pStart = FastMath.max(startRow, p0); final int pEnd = FastMath.min((iBlock + 1) * BLOCK_SIZE, 1 + endRow); for (int p = pStart; p < pEnd; ++p) { for (int jBlock = startColumn / BLOCK_SIZE; jBlock < 1 + endColumn / BLOCK_SIZE; ++jBlock) { final int jWidth = blockWidth(jBlock); final int q0 = jBlock * BLOCK_SIZE; final int qStart = FastMath.max(startColumn, q0); final int qEnd = FastMath.min((jBlock + 1) * BLOCK_SIZE, 1 + endColumn); final double[] block = blocks[iBlock * blockColumns + jBlock]; int k = (p - p0) * jWidth + q

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>Start - q0; for (int q = qStart; q < qEnd; ++q) { block[k] = visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); } /** {@inheritDoc} */ @Override public double walkInRowOrder(final RealMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(rows, columns, startRow, endRow, startColumn, endColumn); for (int iBlock = startRow / BLOCK_SIZE; iBlock < 1 + endRow / BLOCK_SIZE; ++iBlock) { final int p0 = iBlock * BLOCK_SIZE; final int pStart = FastMath.max(startRow, p0); final int pEnd = FastMath.min((iBlock + 1) * BLOCK_SIZE, 1 + endRow); for (int p = pStart; p < pEnd; ++p) { for (int jBlock = startColumn / BLOCK_SIZE; jBlock < 1 + endColumn / BLOCK_SIZE; ++jBlock) { final int jWidth = blockWidth(jBlock); final int q0 = jBlock * BLOCK_SIZE; final int qStart = FastMath.max(startColumn, q0); final int qEnd = FastMath.min((jBlock + 1) * BLOCK_SIZE, 1 + endColumn); final double[] block = blocks[iBlock * blockColumns + jBlock]; int k = (p - p0) * jWidth + qStart - q0; for (int q = qStart; q < qEnd; ++q) { visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); } /** {@inheritDoc} */ @Override public double walkInOptimizedOrder(final RealMatrixChangingVisitor visitor) { visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); int blockIndex = 0; for (int iBlock = 0; iBlock <

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final double[] block = blocks[blockIndex]; int k = 0; for (int p = pStart; p < pEnd; ++p) { for (int q = qStart; q < qEnd; ++q) { block[k] = visitor.visit(p, q, block[k]); ++k; } } ++blockIndex; } } return visitor.end(); } /** {@inheritDoc} */ @Override public double walkInOptimizedOrder(final RealMatrixPreservingVisitor visitor) { visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); int blockIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final double[] block = blocks[blockIndex]; int k = 0; for (int p = pStart; p < pEnd; ++p) { for (int q = qStart; q < qEnd; ++q) { visitor.visit(p, q, block[k]); ++k; } } ++blockIndex; } } return visitor.end(); } /** {@inheritDoc} */ @Override public double walkInOptimizedOrder(final RealMatrixChangingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>); visitor.start(rows, columns, startRow, endRow, startColumn, endColumn); for (int iBlock = startRow / BLOCK_SIZE; iBlock < 1 + endRow / BLOCK_SIZE; ++iBlock) { final int p0 = iBlock * BLOCK_SIZE; final int pStart = FastMath.max(startRow, p0); final int pEnd = FastMath.min((iBlock + 1) * BLOCK_SIZE, 1 + endRow); for (int jBlock = startColumn / BLOCK_SIZE; jBlock < 1 + endColumn / BLOCK_SIZE; ++jBlock) { final int jWidth = blockWidth(jBlock); final int q0 = jBlock * BLOCK_SIZE; final int qStart = FastMath.max(startColumn, q0); final int qEnd = FastMath.min((jBlock + 1) * BLOCK_SIZE, 1 + endColumn); final double[] block = blocks[iBlock * blockColumns + jBlock]; for (int p = pStart; p < pEnd; ++p) { int k = (p - p0) * jWidth + qStart - q0; for (int q = qStart; q < qEnd; ++q) { block[k] = visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); } /** {@inheritDoc} */ @Override public double walkInOptimizedOrder(final RealMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(rows, columns, startRow, endRow, startColumn, endColumn); for (int iBlock = startRow / BLOCK_SIZE; iBlock < 1 + endRow / BLOCK_SIZE; ++iBlock) { final int p0 = iBlock * BLOCK_SIZE; final int pStart = FastMath.max(startRow, p0); final int pEnd = FastMath.min((iBlock + 1) * BLOCK_SIZE, 1 + endRow); for (int jBlock = startColumn / BLOCK_SIZE;

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> jBlock < 1 + endColumn / BLOCK_SIZE; ++jBlock) { final int jWidth = blockWidth(jBlock); final int q0 = jBlock * BLOCK_SIZE; final int qStart = FastMath.max(startColumn, q0); final int qEnd = FastMath.min((jBlock + 1) * BLOCK_SIZE, 1 + endColumn); final double[] block = blocks[iBlock * blockColumns + jBlock]; for (int p = pStart; p < pEnd; ++p) { int k = (p - p0) * jWidth + qStart - q0; for (int q = qStart; q < qEnd; ++q) { visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); } /** * Get the height of a block. * @param blockRow row index (in block sense) of the block * @return height (number of rows) of the block */ private int blockHeight(final int blockRow) { return (blockRow == blockRows - 1) ? rows - blockRow * BLOCK_SIZE : BLOCK_SIZE; } /** * Get the width of a block. * @param blockColumn column index (in block sense) of the block * @return width (number of columns) of the block */ private int blockWidth(final int blockColumn) { return (blockColumn == blockColumns - 1) ? columns - blockColumn * BLOCK_SIZE : BLOCK_SIZE; } }

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> less * than {@code pos + size}. */ public ArrayRealVector(double[] d, int pos, int size) throws NullArgumentException, NumberIsTooLargeException { if (d == null) { throw new NullArgumentException(); } if (d.length < pos + size) { throw new NumberIsTooLargeException(pos + size, d.length, true); } data = new double[size]; System.arraycopy(d, pos, data, 0, size); } /** * Construct a vector from an array. * * @param d Array of {@code Double}s. */ public ArrayRealVector(Double[] d) { data = new double[d.length]; for (int i = 0; i < d.length; i++) { data[i] = d[i].doubleValue(); } } /** * Construct a vector from part of an array. * * @param d Array. * @param pos Position of first entry. * @param size Number of entries to copy. * @throws NullArgumentException if {@code d} is {@code null}. * @throws NumberIsTooLargeException if the size of {@code d} is less * than {@code pos + size}. */ public ArrayRealVector(Double[] d, int pos, int size) throws NullArgumentException, NumberIsTooLargeException { if (d == null) { throw new NullArgumentException(); } if (d.length < pos + size) { throw new NumberIsTooLargeException(pos + size, d.length, true); } data = new double[size]; for (int i = pos; i < pos + size; i++) { data[i - pos] = d[i].doubleValue(); } } /** * Construct a vector from another vector, using a deep copy. * * @param v vector to copy. * @throws NullArgumentException if {@code v} is {@code null}. */ public ArrayRealVector(RealVector v) throws NullArgumentException { if (v == null) { throw new NullArgumentException(); } data = new double[v.getDimension()]; for (int i = 0; i < data.length; ++i) { data[i] = v.getEntry(i); } } /** * Construct a vector from another

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> vector, using a deep copy. * * @param v Vector to copy. * @throws NullArgumentException if {@code v} is {@code null}. */ public ArrayRealVector(ArrayRealVector v) throws NullArgumentException { this(v, true); } /** * Construct a vector from another vector. * * @param v Vector to copy. * @param deep If {@code true} perform a deep copy, otherwise perform a * shallow copy. */ public ArrayRealVector(ArrayRealVector v, boolean deep) { data = deep ? v.data.clone() : v.data; } /** * Construct a vector by appending one vector to another vector. * @param v1 First vector (will be put in front of the new vector). * @param v2 Second vector (will be put at back of the new vector). */ public ArrayRealVector(ArrayRealVector v1, ArrayRealVector v2) { data = new double[v1.data.length + v2.data.length]; System.arraycopy(v1.data, 0, data, 0, v1.data.length); System.arraycopy(v2.data, 0, data, v1.data.length, v2.data.length); } /** * Construct a vector by appending one vector to another vector. * @param v1 First vector (will be put in front of the new vector). * @param v2 Second vector (will be put at back of the new vector). */ public ArrayRealVector(ArrayRealVector v1, RealVector v2) { final int l1 = v1.data.length; final int l2 = v2.getDimension(); data = new double[l1 + l2]; System.arraycopy(v1.data, 0, data, 0, l1); for (int i = 0; i < l2; ++i) { data[l1 + i] = v2.getEntry(i); } } /** * Construct a vector by appending one vector to another vector. * @param v1 First vector (will be put in front of the new vector). * @param v2 Second vector (will be put at back of the new vector). */ public ArrayRealVector(RealVector v1, ArrayRealVector v2) {

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> final int l1 = v1.getDimension(); final int l2 = v2.data.length; data = new double[l1 + l2]; for (int i = 0; i < l1; ++i) { data[i] = v1.getEntry(i); } System.arraycopy(v2.data, 0, data, l1, l2); } /** * Construct a vector by appending one vector to another vector. * @param v1 First vector (will be put in front of the new vector). * @param v2 Second vector (will be put at back of the new vector). */ public ArrayRealVector(ArrayRealVector v1, double[] v2) { final int l1 = v1.getDimension(); final int l2 = v2.length; data = new double[l1 + l2]; System.arraycopy(v1.data, 0, data, 0, l1); System.arraycopy(v2, 0, data, l1, l2); } /** * Construct a vector by appending one vector to another vector. * @param v1 First vector (will be put in front of the new vector). * @param v2 Second vector (will be put at back of the new vector). */ public ArrayRealVector(double[] v1, ArrayRealVector v2) { final int l1 = v1.length; final int l2 = v2.getDimension(); data = new double[l1 + l2]; System.arraycopy(v1, 0, data, 0, l1); System.arraycopy(v2.data, 0, data, l1, l2); } /** * Construct a vector by appending one vector to another vector. * @param v1 first vector (will be put in front of the new vector) * @param v2 second vector (will be put at back of the new vector) */ public ArrayRealVector(double[] v1, double[] v2) { final int l1 = v1.length; final int l2 = v2.length; data = new double[l1 + l2]; System.arraycopy(v1, 0, data, 0, l1); System.arraycopy(v2, 0, data, l

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> function.value(data[i]); } return this; } /** {@inheritDoc} */ @Override public RealVector mapAddToSelf(double d) { for (int i = 0; i < data.length; i++) { data[i] = data[i] + d; } return this; } /** {@inheritDoc} */ @Override public RealVector mapSubtractToSelf(double d) { for (int i = 0; i < data.length; i++) { data[i] = data[i] - d; } return this; } /** {@inheritDoc} */ @Override public RealVector mapMultiplyToSelf(double d) { for (int i = 0; i < data.length; i++) { data[i] = data[i] * d; } return this; } /** {@inheritDoc} */ @Override public RealVector mapDivideToSelf(double d) { for (int i = 0; i < data.length; i++) { data[i] = data[i] / d; } return this; } /** {@inheritDoc} */ @Override public ArrayRealVector ebeMultiply(RealVector v) throws DimensionMismatchException { if (v instanceof ArrayRealVector) { final double[] vData = ((ArrayRealVector) v).data; final int dim = vData.length; checkVectorDimensions(dim); ArrayRealVector result = new ArrayRealVector(dim); double[] resultData = result.data; for (int i = 0; i < dim; i++) { resultData[i] = data[i] * vData[i]; } return result; } else { checkVectorDimensions(v); double[] out = data.clone(); for (int i = 0; i < data.length; i++) { out[i] *= v.getEntry(i); } return new ArrayRealVector(out, false); } } /** {@inheritDoc} */ @Override public ArrayRealVector ebeDivide(RealVector v) throws DimensionMismatchException { if (v instanceof ArrayRealVector) { final double[] vData = ((ArrayRealVector) v).data; final int dim = vData.length; checkVectorDimensions(dim); ArrayRealVector result

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> = new ArrayRealVector(dim); double[] resultData = result.data; for (int i = 0; i < dim; i++) { resultData[i] = data[i] / vData[i]; } return result; } else { checkVectorDimensions(v); double[] out = data.clone(); for (int i = 0; i < data.length; i++) { out[i] /= v.getEntry(i); } return new ArrayRealVector(out, false); } } /** * Get a reference to the underlying data array. * This method does not make a fresh copy of the underlying data. * * @return the array of entries. */ public double[] getDataRef() { return data; } /** {@inheritDoc} */ @Override public double dotProduct(RealVector v) throws DimensionMismatchException { if (v instanceof ArrayRealVector) { final double[] vData = ((ArrayRealVector) v).data; checkVectorDimensions(vData.length); double dot = 0; for (int i = 0; i < data.length; i++) { dot += data[i] * vData[i]; } return dot; } return super.dotProduct(v); } /** {@inheritDoc} */ @Override public double getNorm() { double sum = 0; for (double a : data) { sum += a * a; } return FastMath.sqrt(sum); } /** {@inheritDoc} */ @Override public double getL1Norm() { double sum = 0; for (double a : data) { sum += FastMath.abs(a); } return sum; } /** {@inheritDoc} */ @Override public double getLInfNorm() { double max = 0; for (double a : data) { max = FastMath.max(max, FastMath.abs(a)); } return max; } /** {@inheritDoc} */ @Override public double getDistance(RealVector v) throws DimensionMismatchException { if (v instanceof ArrayRealVector) { final double[] vData = ((ArrayRealVector) v).data; checkVectorDimensions(vData.length); double sum = 0; for (int i = 0;

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> i < data.length; ++i) { final double delta = data[i] - vData[i]; sum += delta * delta; } return FastMath.sqrt(sum); } else { checkVectorDimensions(v); double sum = 0; for (int i = 0; i < data.length; ++i) { final double delta = data[i] - v.getEntry(i); sum += delta * delta; } return FastMath.sqrt(sum); } } /** {@inheritDoc} */ @Override public double getL1Distance(RealVector v) throws DimensionMismatchException { if (v instanceof ArrayRealVector) { final double[] vData = ((ArrayRealVector) v).data; checkVectorDimensions(vData.length); double sum = 0; for (int i = 0; i < data.length; ++i) { final double delta = data[i] - vData[i]; sum += FastMath.abs(delta); } return sum; } else { checkVectorDimensions(v); double sum = 0; for (int i = 0; i < data.length; ++i) { final double delta = data[i] - v.getEntry(i); sum += FastMath.abs(delta); } return sum; } } /** {@inheritDoc} */ @Override public double getLInfDistance(RealVector v) throws DimensionMismatchException { if (v instanceof ArrayRealVector) { final double[] vData = ((ArrayRealVector) v).data; checkVectorDimensions(vData.length); double max = 0; for (int i = 0; i < data.length; ++i) { final double delta = data[i] - vData[i]; max = FastMath.max(max, FastMath.abs(delta)); } return max; } else { checkVectorDimensions(v); double max = 0; for (int i = 0; i < data.length; ++i) { final double delta = data[i] - v.getEntry(i); max = FastMath.max(max, FastMath.abs(delta)); } return max; } } /** {@inheritDoc} */ @Override public RealMatrix outerProduct(Real

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>Vector v) { if (v instanceof ArrayRealVector) { final double[] vData = ((ArrayRealVector) v).data; final int m = data.length; final int n = vData.length; final RealMatrix out = MatrixUtils.createRealMatrix(m, n); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { out.setEntry(i, j, data[i] * vData[j]); } } return out; } else { final int m = data.length; final int n = v.getDimension(); final RealMatrix out = MatrixUtils.createRealMatrix(m, n); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { out.setEntry(i, j, data[i] * v.getEntry(j)); } } return out; } } /** {@inheritDoc} */ @Override public double getEntry(int index) throws OutOfRangeException { try { return data[index]; } catch (IndexOutOfBoundsException e) { throw new OutOfRangeException(LocalizedFormats.INDEX, index, 0, getDimension() - 1); } } /** {@inheritDoc} */ @Override public int getDimension() { return data.length; } /** {@inheritDoc} */ @Override public RealVector append(RealVector v) { try { return new ArrayRealVector(this, (ArrayRealVector) v); } catch (ClassCastException cce) { return new ArrayRealVector(this, v); } } /** * Construct a vector by appending a vector to this vector. * * @param v Vector to append to this one. * @return a new vector. */ public ArrayRealVector append(ArrayRealVector v) { return new ArrayRealVector(this, v); } /** {@inheritDoc} */ @Override public RealVector append(double in) { final double[] out = new double[data.length + 1]; System.arraycopy(data, 0, out, 0, data.length); out[data.length] = in; return new ArrayRealVector(out, false); }

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> /** {@inheritDoc} */ @Override public RealVector getSubVector(int index, int n) throws OutOfRangeException, NotPositiveException { if (n < 0) { throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n); } ArrayRealVector out = new ArrayRealVector(n); try { System.arraycopy(data, index, out.data, 0, n); } catch (IndexOutOfBoundsException e) { checkIndex(index); checkIndex(index + n - 1); } return out; } /** {@inheritDoc} */ @Override public void setEntry(int index, double value) throws OutOfRangeException { try { data[index] = value; } catch (IndexOutOfBoundsException e) { checkIndex(index); } } /** {@inheritDoc} */ @Override public void addToEntry(int index, double increment) throws OutOfRangeException { try { data[index] += increment; } catch(IndexOutOfBoundsException e){ throw new OutOfRangeException(LocalizedFormats.INDEX, index, 0, data.length - 1); } } /** {@inheritDoc} */ @Override public void setSubVector(int index, RealVector v) throws OutOfRangeException { if (v instanceof ArrayRealVector) { setSubVector(index, ((ArrayRealVector) v).data); } else { try { for (int i = index; i < index + v.getDimension(); ++i) { data[i] = v.getEntry(i - index); } } catch (IndexOutOfBoundsException e) { checkIndex(index); checkIndex(index + v.getDimension() - 1); } } } /** * Set a set of consecutive elements. * * @param index Index of first element to be set. * @param v Vector containing the values to set. * @throws OutOfRangeException if the index is inconsistent with the vector * size. */ public void setSubVector(int index, double[] v) throws OutOfRangeException { try { System.arraycopy(v, 0, data, index, v.length); } catch (IndexOutOfBoundsException e) { checkIndex(index); checkIndex(index + v.length -

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>; } if (rhs.isNaN()) { return this.isNaN(); } for (int i = 0; i < data.length; ++i) { if (data[i] != rhs.getEntry(i)) { return false; } } return true; } /** * {@inheritDoc} All {@code NaN} values have the same hash code. */ @Override public int hashCode() { if (isNaN()) { return 9; } return MathUtils.hash(data); } /** {@inheritDoc} */ @Override public ArrayRealVector combine(double a, double b, RealVector y) throws DimensionMismatchException { return copy().combineToSelf(a, b, y); } /** {@inheritDoc} */ @Override public ArrayRealVector combineToSelf(double a, double b, RealVector y) throws DimensionMismatchException { if (y instanceof ArrayRealVector) { final double[] yData = ((ArrayRealVector) y).data; checkVectorDimensions(yData.length); for (int i = 0; i < this.data.length; i++) { data[i] = a * data[i] + b * yData[i]; } } else { checkVectorDimensions(y); for (int i = 0; i < this.data.length; i++) { data[i] = a * data[i] + b * y.getEntry(i); } } return this; } /** {@inheritDoc} */ @Override public double walkInDefaultOrder(final RealVectorPreservingVisitor visitor) { visitor.start(data.length, 0, data.length - 1); for (int i = 0; i < data.length; i++) { visitor.visit(i, data[i]); } return visitor.end(); } /** {@inheritDoc} */ @Override public double walkInDefaultOrder(final RealVectorPreservingVisitor visitor, final int start, final int end) throws NumberIsTooSmallException, OutOfRangeException { checkIndices(start, end); visitor.start(data.length, start, end); for (int i = start; i <= end; i++) { visitor.visit(i, data[i]); } return visitor.end(); } /** *

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> within a given relative tolerance. * * @param matrix Matrix to check. * @param relativeTolerance Tolerance of the symmetry check. * @param raiseException If {@code true}, an exception will be raised if * the matrix is not symmetric. * @return {@code true} if {@code matrix} is symmetric. * @throws NonSquareMatrixException if the matrix is not square. * @throws NonSymmetricMatrixException if the matrix is not symmetric. */ private static boolean isSymmetricInternal(RealMatrix matrix, double relativeTolerance, boolean raiseException) { final int rows = matrix.getRowDimension(); if (rows != matrix.getColumnDimension()) { if (raiseException) { throw new NonSquareMatrixException(rows, matrix.getColumnDimension()); } else { return false; } } for (int i = 0; i < rows; i++) { for (int j = i + 1; j < rows; j++) { final double mij = matrix.getEntry(i, j); final double mji = matrix.getEntry(j, i); if (FastMath.abs(mij - mji) > FastMath.max(FastMath.abs(mij), FastMath.abs(mji)) * relativeTolerance) { if (raiseException) { throw new NonSymmetricMatrixException(i, j, relativeTolerance); } else { return false; } } } } return true; } /** * Checks whether a matrix is symmetric. * * @param matrix Matrix to check. * @param eps Relative tolerance. * @throws NonSquareMatrixException if the matrix is not square. * @throws NonSymmetricMatrixException if the matrix is not symmetric. * @since 3.1 */ public static void checkSymmetric(RealMatrix matrix, double eps) { isSymmetricInternal(matrix, eps, true); } /** * Checks whether a matrix is symmetric. * * @param matrix Matrix to check. * @param eps Relative tolerance. * @return {@code true} if {@code matrix} is symmetric. * @since 3.1 */ public static boolean isSymmetric(RealMatrix matrix, double eps) { return isSymmetricInternal(matrix, eps, false); } /** * Check if matrix indices are valid. * * @param m Matrix.

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> * public class NamedVector implements Serializable { * * private final String name; * private final transient RealVector coefficients; * * // omitted constructors, getters ... * * private void writeObject(ObjectOutputStream oos) throws IOException { * oos.defaultWriteObject(); // takes care of name field * MatrixUtils.serializeRealVector(coefficients, oos); * } * * private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { * ois.defaultReadObject(); // takes care of name field * MatrixUtils.deserializeRealVector(this, "coefficients", ois); * } * * } * </code></pre> * </p> * * @param vector real vector to serialize * @param oos stream where the real vector should be written * @exception IOException if object cannot be written to stream * @see #deserializeRealVector(Object, String, ObjectInputStream) */ public static void serializeRealVector(final RealVector vector, final ObjectOutputStream oos) throws IOException { final int n = vector.getDimension(); oos.writeInt(n); for (int i = 0; i < n; ++i) { oos.writeDouble(vector.getEntry(i)); } } /** Deserialize a {@link RealVector} field in a class. * <p> * This method is intended to be called from within a private * <code>readObject</code> method (after a call to * <code>ois.defaultReadObject()</code>) in a class that has a * {@link RealVector} field, which should be declared <code>transient</code>. * This way, the default handling does not deserialize the vector (the {@link * RealVector} interface is not serializable by default) but this method does * deserialize it specifically. * </p> * @param instance instance in which the field must be set up * @param fieldName name of the field within the class (may be private and final) * @param ois stream from which the real vector should be read * @exception ClassNotFoundException if a class in the stream cannot be found * @exception IOException if object cannot be read from the stream * @see #serializeRealVector(RealVector, ObjectOutputStream) */ public static void

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>is); * } * * } * </code></pre> * </p> * * @param matrix real matrix to serialize * @param oos stream where the real matrix should be written * @exception IOException if object cannot be written to stream * @see #deserializeRealMatrix(Object, String, ObjectInputStream) */ public static void serializeRealMatrix(final RealMatrix matrix, final ObjectOutputStream oos) throws IOException { final int n = matrix.getRowDimension(); final int m = matrix.getColumnDimension(); oos.writeInt(n); oos.writeInt(m); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { oos.writeDouble(matrix.getEntry(i, j)); } } } /** Deserialize a {@link RealMatrix} field in a class. * <p> * This method is intended to be called from within a private * <code>readObject</code> method (after a call to * <code>ois.defaultReadObject()</code>) in a class that has a * {@link RealMatrix} field, which should be declared <code>transient</code>. * This way, the default handling does not deserialize the matrix (the {@link * RealMatrix} interface is not serializable by default) but this method does * deserialize it specifically. * </p> * @param instance instance in which the field must be set up * @param fieldName name of the field within the class (may be private and final) * @param ois stream from which the real matrix should be read * @exception ClassNotFoundException if a class in the stream cannot be found * @exception IOException if object cannot be read from the stream * @see #serializeRealMatrix(RealMatrix, ObjectOutputStream) */ public static void deserializeRealMatrix(final Object instance, final String fieldName, final ObjectInputStream ois) throws ClassNotFoundException, IOException { try { // read the matrix data final int n = ois.readInt(); final int m = ois.readInt(); final double[][] data = new double[n][m]; for (int i = 0; i < n; ++i) { final double[] dataI = data[i]; for (int j = 0; j < m; ++

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>getEntry(i, i); if( FastMath.abs(diag) < Precision.SAFE_MIN ){ throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR); } double bi = b.getEntry(i)/diag; b.setEntry(i, bi ); for( int j = i+1; j< rows; j++ ){ b.setEntry(j, b.getEntry(j)-bi*rm.getEntry(j,i) ); } } } /** Solver a system composed of an Upper Triangular Matrix * {@link RealMatrix}. * <p> * This method is called to solve systems of equations which are * of the lower triangular form. The matrix {@link RealMatrix} * is assumed, though not checked, to be in upper triangular form. * The vector {@link RealVector} is overwritten with the solution. * The matrix is checked that it is square and its dimensions match * the length of the vector. * </p> * @param rm RealMatrix which is upper triangular * @param b RealVector this is overwritten * @throws DimensionMismatchException if the matrix and vector are not * conformable * @throws NonSquareMatrixException if the matrix {@code rm} is not * square * @throws MathArithmeticException if the absolute value of one of the diagonal * coefficient of {@code rm} is lower than {@link Precision#SAFE_MIN} */ public static void solveUpperTriangularSystem(RealMatrix rm, RealVector b) throws DimensionMismatchException, MathArithmeticException, NonSquareMatrixException { if ((rm == null) || (b == null) || ( rm.getRowDimension() != b.getDimension())) { throw new DimensionMismatchException( (rm == null) ? 0 : rm.getRowDimension(), (b == null) ? 0 : b.getDimension()); } if( rm.getColumnDimension() != rm.getRowDimension() ){ throw new NonSquareMatrixException(rm.getRowDimension(), rm.getColumnDimension()); } int rows = rm.getRowDimension(); for( int i = rows-1 ; i >-1 ; i-- ){ double diag = rm.getEntry(i, i); if( FastMath.abs(diag) < Precision.SAFE_MIN ){ throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> } double bi = b.getEntry(i)/diag; b.setEntry(i, bi ); for( int j = i-1; j>-1; j-- ){ b.setEntry(j, b.getEntry(j)-bi*rm.getEntry(j,i) ); } } } /** * Computes the inverse of the given matrix by splitting it into * 4 sub-matrices. * * @param m Matrix whose inverse must be computed. * @param splitIndex Index that determines the "split" line and * column. * The element corresponding to this index will part of the * upper-left sub-matrix. * @return the inverse of {@code m}. * @throws NonSquareMatrixException if {@code m} is not square. */ public static RealMatrix blockInverse(RealMatrix m, int splitIndex) { final int n = m.getRowDimension(); if (m.getColumnDimension() != n) { throw new NonSquareMatrixException(m.getRowDimension(), m.getColumnDimension()); } final int splitIndex1 = splitIndex + 1; final RealMatrix a = m.getSubMatrix(0, splitIndex, 0, splitIndex); final RealMatrix b = m.getSubMatrix(0, splitIndex, splitIndex1, n - 1); final RealMatrix c = m.getSubMatrix(splitIndex1, n - 1, 0, splitIndex); final RealMatrix d = m.getSubMatrix(splitIndex1, n - 1, splitIndex1, n - 1); final SingularValueDecomposition aDec = new SingularValueDecomposition(a); final RealMatrix aInv = aDec.getSolver().getInverse(); final SingularValueDecomposition dDec = new SingularValueDecomposition(d); final RealMatrix dInv = dDec.getSolver().getInverse(); final RealMatrix tmp1 = a.subtract(b.multiply(dInv).multiply(c)); final SingularValueDecomposition tmp1Dec = new SingularValueDecomposition(tmp1); final RealMatrix result00 = tmp1Dec.getSolver().getInverse(); final RealMatrix tmp2 = d.subtract(c.multiply(aInv).multiply(b)); final SingularValueDecomposition tmp2Dec = new SingularValueDecomposition(tmp2); final RealMatrix result

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>); } } /** {@inheritDoc} */ @Override public double getEntry(final int row, final int column) throws OutOfRangeException { MatrixUtils.checkMatrixIndex(this, row, column); return data[row][column]; } /** {@inheritDoc} */ @Override public void setEntry(final int row, final int column, final double value) throws OutOfRangeException { MatrixUtils.checkMatrixIndex(this, row, column); data[row][column] = value; } /** {@inheritDoc} */ @Override public void addToEntry(final int row, final int column, final double increment) throws OutOfRangeException { MatrixUtils.checkMatrixIndex(this, row, column); data[row][column] += increment; } /** {@inheritDoc} */ @Override public void multiplyEntry(final int row, final int column, final double factor) throws OutOfRangeException { MatrixUtils.checkMatrixIndex(this, row, column); data[row][column] *= factor; } /** {@inheritDoc} */ @Override public int getRowDimension() { return (data == null) ? 0 : data.length; } /** {@inheritDoc} */ @Override public int getColumnDimension() { return ((data == null) || (data[0] == null)) ? 0 : data[0].length; } /** {@inheritDoc} */ @Override public double[] operate(final double[] v) throws DimensionMismatchException { final int nRows = this.getRowDimension(); final int nCols = this.getColumnDimension(); if (v.length != nCols) { throw new DimensionMismatchException(v.length, nCols); } final double[] out = new double[nRows]; for (int row = 0; row < nRows; row++) { final double[] dataRow = data[row]; double sum = 0; for (int i = 0; i < nCols; i++) { sum += dataRow[i] * v[i]; } out[row] = sum; } return out; } /** {@inheritDoc} */ @Override public double[] preMultiply(final double[] v) throws DimensionMismatchException { final int nRows = getRowDimension(); final int nCols = getColumn

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> the range of allowed * error (inclusive). * * @param x first value * @param y second value * @param eps the amount of absolute error to allow. * @return {@code true} if the values are equal or within range of each other. * @since 2.2 */ public static boolean equals(float x, float y, float eps) { return equals(x, y, 1) || FastMath.abs(y - x) <= eps; } /** * Returns true if both arguments are NaN or are equal or within the range * of allowed error (inclusive). * * @param x first value * @param y second value * @param eps the amount of absolute error to allow. * @return {@code true} if the values are equal or within range of each other, * or both are NaN. * @since 2.2 */ public static boolean equalsIncludingNaN(float x, float y, float eps) { return equalsIncludingNaN(x, y) || (FastMath.abs(y - x) <= eps); } /** * Returns true if both arguments are equal or within the range of allowed * error (inclusive). * Two float numbers are considered equal if there are {@code (maxUlps - 1)} * (or fewer) floating point numbers between them, i.e. two adjacent floating * point numbers are considered equal. * Adapted from <a * href="http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm"> * Bruce Dawson</a> * * @param x first value * @param y second value * @param maxUlps {@code (maxUlps - 1)} is the number of floating point * values between {@code x} and {@code y}. * @return {@code true} if there are fewer than {@code maxUlps} floating * point values between {@code x} and {@code y}. * @since 2.2 */ public static boolean equals(float x, float y, int maxUlps) { int xInt = Float.floatToIntBits(x); int yInt = Float.floatToIntBits(y); // Make lexicographically ordered as a two's-complement integer. if (xInt < 0) {

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> xInt = SGN_MASK_FLOAT - xInt; } if (yInt < 0) { yInt = SGN_MASK_FLOAT - yInt; } final boolean isEqual = FastMath.abs(xInt - yInt) <= maxUlps; return isEqual && !Float.isNaN(x) && !Float.isNaN(y); } /** * Returns true if both arguments are NaN or if they are equal as defined * by {@link #equals(float,float,int) equals(x, y, maxUlps)}. * * @param x first value * @param y second value * @param maxUlps {@code (maxUlps - 1)} is the number of floating point * values between {@code x} and {@code y}. * @return {@code true} if both arguments are NaN or if there are less than * {@code maxUlps} floating point values between {@code x} and {@code y}. * @since 2.2 */ public static boolean equalsIncludingNaN(float x, float y, int maxUlps) { return (Float.isNaN(x) && Float.isNaN(y)) || equals(x, y, maxUlps); } /** * Returns true iff they are equal as defined by * {@link #equals(double,double,int) equals(x, y, 1)}. * * @param x first value * @param y second value * @return {@code true} if the values are equal. */ public static boolean equals(double x, double y) { return equals(x, y, 1); } /** * Returns true if both arguments are NaN or neither is NaN and they are * equal as defined by {@link #equals(double,double) equals(x, y, 1)}. * * @param x first value * @param y second value * @return {@code true} if the values are equal or both are NaN. * @since 2.2 */ public static boolean equalsIncludingNaN(double x, double y) { return (Double.isNaN(x) && Double.isNaN(y)) || equals(x, y, 1); } /** * Returns {@code true} if there is no double value strictly between the * arguments or the difference between them is within the range of allowed

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> * error (inclusive). * * @param x First value. * @param y Second value. * @param eps Amount of allowed absolute error. * @return {@code true} if the values are two adjacent floating point * numbers or they are within range of each other. */ public static boolean equals(double x, double y, double eps) { return equals(x, y, 1) || FastMath.abs(y - x) <= eps; } /** * Returns {@code true} if there is no double value strictly between the * arguments or the reltaive difference between them is smaller or equal * to the given tolerance. * * @param x First value. * @param y Second value. * @param eps Amount of allowed relative error. * @return {@code true} if the values are two adjacent floating point * numbers or they are within range of each other. * @since 3.1 */ public static boolean equalsWithRelativeTolerance(double x, double y, double eps) { if (equals(x, y, 1)) { return true; } final double absoluteMax = FastMath.max(FastMath.abs(x), FastMath.abs(y)); final double relativeDifference = FastMath.abs((x - y) / absoluteMax); return relativeDifference <= eps; } /** * Returns true if both arguments are NaN or are equal or within the range * of allowed error (inclusive). * * @param x first value * @param y second value * @param eps the amount of absolute error to allow. * @return {@code true} if the values are equal or within range of each other, * or both are NaN. * @since 2.2 */ public static boolean equalsIncludingNaN(double x, double y, double eps) { return equalsIncludingNaN(x, y) || (FastMath.abs(y - x) <= eps); } /** * Returns true if both arguments are equal or within the range of allowed * error (inclusive). * Two float numbers are considered equal if there are {@code (maxUlps - 1)} * (or fewer) floating point numbers between them, i.e. two adjacent floating * point numbers are considered equal. * Adapted from <a * href="http://www.cygnus-software.com

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>/papers/comparingfloats/comparingfloats.htm"> * Bruce Dawson</a> * * @param x first value * @param y second value * @param maxUlps {@code (maxUlps - 1)} is the number of floating point * values between {@code x} and {@code y}. * @return {@code true} if there are fewer than {@code maxUlps} floating * point values between {@code x} and {@code y}. */ public static boolean equals(double x, double y, int maxUlps) { long xInt = Double.doubleToLongBits(x); long yInt = Double.doubleToLongBits(y); // Make lexicographically ordered as a two's-complement integer. if (xInt < 0) { xInt = SGN_MASK - xInt; } if (yInt < 0) { yInt = SGN_MASK - yInt; } final boolean isEqual = FastMath.abs(xInt - yInt) <= maxUlps; return isEqual && !Double.isNaN(x) && !Double.isNaN(y); } /** * Returns true if both arguments are NaN or if they are equal as defined * by {@link #equals(double,double,int) equals(x, y, maxUlps)}. * * @param x first value * @param y second value * @param maxUlps {@code (maxUlps - 1)} is the number of floating point * values between {@code x} and {@code y}. * @return {@code true} if both arguments are NaN or if there are less than * {@code maxUlps} floating point values between {@code x} and {@code y}. * @since 2.2 */ public static boolean equalsIncludingNaN(double x, double y, int maxUlps) { return (Double.isNaN(x) && Double.isNaN(y)) || equals(x, y, maxUlps); } /** * Rounds the given value to the specified number of decimal places. * The value is rounded using the {@link BigDecimal#ROUND_HALF_UP} method. * * @param x Value to round. * @param scale Number of digits to the right of the decimal point. * @return the rounded value.

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> scale Number of digits to the right of the decimal point. * @param roundingMethod Rounding method as defined in {@link BigDecimal}. * @return the rounded value. * @since 1.1 (previously in {@code MathUtils}, moved as of version 3.0) * @throws MathArithmeticException if an exact operation is required but result is not exact * @throws MathIllegalArgumentException if {@code roundingMethod} is not a valid rounding method. */ public static float round(float x, int scale, int roundingMethod) throws MathArithmeticException, MathIllegalArgumentException { final float sign = FastMath.copySign(1f, x); final float factor = (float) FastMath.pow(10.0f, scale) * sign; return (float) roundUnscaled(x * factor, sign, roundingMethod) / factor; } /** * Rounds the given non-negative value to the "nearest" integer. Nearest is * determined by the rounding method specified. Rounding methods are defined * in {@link BigDecimal}. * * @param unscaled Value to round. * @param sign Sign of the original, scaled value. * @param roundingMethod Rounding method, as defined in {@link BigDecimal}. * @return the rounded value. * @throws MathArithmeticException if an exact operation is required but result is not exact * @throws MathIllegalArgumentException if {@code roundingMethod} is not a valid rounding method. * @since 1.1 (previously in {@code MathUtils}, moved as of version 3.0) */ private static double roundUnscaled(double unscaled, double sign, int roundingMethod) throws MathArithmeticException, MathIllegalArgumentException { switch (roundingMethod) { case BigDecimal.ROUND_CEILING : if (sign == -1) { unscaled = FastMath.floor(FastMath.nextAfter(unscaled, Double.NEGATIVE_INFINITY)); } else { unscaled = FastMath.ceil(FastMath.nextAfter(unscaled, Double.POSITIVE_INFINITY)); } break; case BigDecimal.ROUND_DOWN : unscaled = FastMath.floor(FastMath.nextAfter(unscaled, Double.NEGATIVE_INFINITY)); break; case BigDecimal.ROUND_FLOOR : if (sign == -1) { unscaled = FastMath.ceil(FastMath.nextAfter(unscaled, Double.POSITIVE_

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>INFINITY)); } else { unscaled = FastMath.floor(FastMath.nextAfter(unscaled, Double.NEGATIVE_INFINITY)); } break; case BigDecimal.ROUND_HALF_DOWN : { unscaled = FastMath.nextAfter(unscaled, Double.NEGATIVE_INFINITY); double fraction = unscaled - FastMath.floor(unscaled); if (fraction > 0.5) { unscaled = FastMath.ceil(unscaled); } else { unscaled = FastMath.floor(unscaled); } break; } case BigDecimal.ROUND_HALF_EVEN : { double fraction = unscaled - FastMath.floor(unscaled); if (fraction > 0.5) { unscaled = FastMath.ceil(unscaled); } else if (fraction < 0.5) { unscaled = FastMath.floor(unscaled); } else { // The following equality test is intentional and needed for rounding purposes if (FastMath.floor(unscaled) / 2.0 == FastMath.floor(Math .floor(unscaled) / 2.0)) { // even unscaled = FastMath.floor(unscaled); } else { // odd unscaled = FastMath.ceil(unscaled); } } break; } case BigDecimal.ROUND_HALF_UP : { unscaled = FastMath.nextAfter(unscaled, Double.POSITIVE_INFINITY); double fraction = unscaled - FastMath.floor(unscaled); if (fraction >= 0.5) { unscaled = FastMath.ceil(unscaled); } else { unscaled = FastMath.floor(unscaled); } break; } case BigDecimal.ROUND_UNNECESSARY : if (unscaled != FastMath.floor(unscaled)) { throw new MathArithmeticException(); } break; case BigDecimal.ROUND_UP : unscaled = FastMath.ceil(FastMath.nextAfter(unscaled, Double.POSITIVE_INFINITY)); break; default : throw new MathIllegalArgumentException(LocalizedFormats.INVALID_ROUNDING_METHOD, roundingMethod, "ROUND_CEILING", BigDecimal.ROUND_CEILING, "ROUND_DOWN", BigDecimal.ROUND_DOWN, "ROUND_FLOOR", BigDecimal.ROUND_FLOOR, "ROUND_HALF_DOWN

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>. * @param v vector with which outer product should be computed * @return the matrix outer product between instance and v */ FieldMatrix<T> outerProduct(FieldVector<T> v); /** * Returns the entry in the specified index. * * @param index Index location of entry to be fetched. * @return the vector entry at {@code index}. * @throws OutOfRangeException if the index is not valid. * @see #setEntry(int, FieldElement) */ T getEntry(int index) throws OutOfRangeException; /** * Set a single element. * @param index element index. * @param value new value for the element. * @throws OutOfRangeException if the index is not valid. * @see #getEntry(int) */ void setEntry(int index, T value) throws OutOfRangeException; /** * Returns the size of the vector. * @return size */ int getDimension(); /** * Construct a vector by appending a vector to this vector. * @param v vector to append to this one. * @return a new vector */ FieldVector<T> append(FieldVector<T> v); /** * Construct a vector by appending a T to this vector. * @param d T to append. * @return a new vector */ FieldVector<T> append(T d); /** * Get a subvector from consecutive elements. * @param index index of first element. * @param n number of elements to be retrieved. * @return a vector containing n elements. * @throws OutOfRangeException if the index is not valid. * @throws NotPositiveException if the number of elements if not positive. */ FieldVector<T> getSubVector(int index, int n) throws OutOfRangeException, NotPositiveException; /** * Set a set of consecutive elements. * @param index index of first element to be set. * @param v vector containing the values to set. * @throws OutOfRangeException if the index is not valid. */ void setSubVector(int index, FieldVector<T> v) throws OutOfRangeException; /** * Set all elements to a single value. * @param value single value to set for all elements */ void set(T value); /** * Convert

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> * be rectangular to fit matrix size) and each block is stored in a flattened * one-dimensional array. * </p> * <p> * This method creates an array in blocks layout from an input array in raw layout. * It can be used to provide the array argument of the {@link * #BlockFieldMatrix(int, int, FieldElement[][], boolean)} * constructor. * </p> * @param <T> Type of the field elements. * @param rawData Data array in raw layout. * @return a new data array containing the same entries but in blocks layout * @throws DimensionMismatchException if {@code rawData} is not rectangular * (not all rows have the same length). * @see #createBlocksLayout(Field, int, int) * @see #BlockFieldMatrix(int, int, FieldElement[][], boolean) */ public static <T extends FieldElement<T>> T[][] toBlocksLayout(final T[][] rawData) throws DimensionMismatchException { final int rows = rawData.length; final int columns = rawData[0].length; final int blockRows = (rows + BLOCK_SIZE - 1) / BLOCK_SIZE; final int blockColumns = (columns + BLOCK_SIZE - 1) / BLOCK_SIZE; // safety checks for (int i = 0; i < rawData.length; ++i) { final int length = rawData[i].length; if (length != columns) { throw new DimensionMismatchException(columns, length); } } // convert array final Field<T> field = extractField(rawData); final T[][] blocks = buildArray(field, blockRows * blockColumns, -1); int blockIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); final int iHeight = pEnd - pStart; for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final int jWidth = qEnd - q

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>Start; // allocate new block final T[] block = buildArray(field, iHeight * jWidth); blocks[blockIndex] = block; // copy data int index = 0; for (int p = pStart; p < pEnd; ++p) { System.arraycopy(rawData[p], qStart, block, index, jWidth); index += jWidth; } ++blockIndex; } } return blocks; } /** * Create a data array in blocks layout. * <p> * This method can be used to create the array argument of the {@link * #BlockFieldMatrix(int, int, FieldElement[][], boolean)} * constructor. * </p> * @param <T> Type of the field elements. * @param field Field to which the elements belong. * @param rows Number of rows in the new matrix. * @param columns Number of columns in the new matrix. * @return a new data array in blocks layout. * @see #toBlocksLayout(FieldElement[][]) * @see #BlockFieldMatrix(int, int, FieldElement[][], boolean) */ public static <T extends FieldElement<T>> T[][] createBlocksLayout(final Field<T> field, final int rows, final int columns) { final int blockRows = (rows + BLOCK_SIZE - 1) / BLOCK_SIZE; final int blockColumns = (columns + BLOCK_SIZE - 1) / BLOCK_SIZE; final T[][] blocks = buildArray(field, blockRows * blockColumns, -1); int blockIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); final int iHeight = pEnd - pStart; for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final int jWidth = qEnd - qStart; blocks[blockIndex] = buildArray(field, iHeight * jWidth); ++blockIndex; } } return blocks;

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> } /** {@inheritDoc} */ @Override public FieldMatrix<T> createMatrix(final int rowDimension, final int columnDimension) throws NotStrictlyPositiveException { return new BlockFieldMatrix<T>(getField(), rowDimension, columnDimension); } /** {@inheritDoc} */ @Override public FieldMatrix<T> copy() { // create an empty matrix BlockFieldMatrix<T> copied = new BlockFieldMatrix<T>(getField(), rows, columns); // copy the blocks for (int i = 0; i < blocks.length; ++i) { System.arraycopy(blocks[i], 0, copied.blocks[i], 0, blocks[i].length); } return copied; } /** {@inheritDoc} */ @Override public FieldMatrix<T> add(final FieldMatrix<T> m) throws MatrixDimensionMismatchException { try { return add((BlockFieldMatrix<T>) m); } catch (ClassCastException cce) { // safety check checkAdditionCompatible(m); final BlockFieldMatrix<T> out = new BlockFieldMatrix<T>(getField(), rows, columns); // perform addition block-wise, to ensure good cache behavior int blockIndex = 0; for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) { for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) { // perform addition on the current block final T[] outBlock = out.blocks[blockIndex]; final T[] tBlock = blocks[blockIndex]; final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); int k = 0; for (int p = pStart; p < pEnd; ++p) { for (int q = qStart; q < qEnd; ++q) { outBlock[k] = tBlock[k].add(m.getEntry(p, q)); ++k; } } // go to next block ++blockIndex; } } return out; } } /** * Compute the

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> sum of {@code this} and {@code m}. * * @param m matrix to be added * @return {@code this + m} * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this} */ public BlockFieldMatrix<T> add(final BlockFieldMatrix<T> m) throws MatrixDimensionMismatchException { // safety check checkAdditionCompatible(m); final BlockFieldMatrix<T> out = new BlockFieldMatrix<T>(getField(), rows, columns); // perform addition block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < out.blocks.length; ++blockIndex) { final T[] outBlock = out.blocks[blockIndex]; final T[] tBlock = blocks[blockIndex]; final T[] mBlock = m.blocks[blockIndex]; for (int k = 0; k < outBlock.length; ++k) { outBlock[k] = tBlock[k].add(mBlock[k]); } } return out; } /** {@inheritDoc} */ @Override public FieldMatrix<T> subtract(final FieldMatrix<T> m) throws MatrixDimensionMismatchException { try { return subtract((BlockFieldMatrix<T>) m); } catch (ClassCastException cce) { // safety check checkSubtractionCompatible(m); final BlockFieldMatrix<T> out = new BlockFieldMatrix<T>(getField(), rows, columns); // perform subtraction block-wise, to ensure good cache behavior int blockIndex = 0; for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) { for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) { // perform subtraction on the current block final T[] outBlock = out.blocks[blockIndex]; final T[] tBlock = blocks[blockIndex]; final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); int k = 0; for (int p = pStart; p

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> < pEnd; ++p) { for (int q = qStart; q < qEnd; ++q) { outBlock[k] = tBlock[k].subtract(m.getEntry(p, q)); ++k; } } // go to next block ++blockIndex; } } return out; } } /** * Compute {@code this - m}. * * @param m matrix to be subtracted * @return {@code this - m} * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this} */ public BlockFieldMatrix<T> subtract(final BlockFieldMatrix<T> m) throws MatrixDimensionMismatchException { // safety check checkSubtractionCompatible(m); final BlockFieldMatrix<T> out = new BlockFieldMatrix<T>(getField(), rows, columns); // perform subtraction block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < out.blocks.length; ++blockIndex) { final T[] outBlock = out.blocks[blockIndex]; final T[] tBlock = blocks[blockIndex]; final T[] mBlock = m.blocks[blockIndex]; for (int k = 0; k < outBlock.length; ++k) { outBlock[k] = tBlock[k].subtract(mBlock[k]); } } return out; } /** {@inheritDoc} */ @Override public FieldMatrix<T> scalarAdd(final T d) { final BlockFieldMatrix<T> out = new BlockFieldMatrix<T>(getField(), rows, columns); // perform subtraction block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < out.blocks.length; ++blockIndex) { final T[] outBlock = out.blocks[blockIndex]; final T[] tBlock = blocks[blockIndex]; for (int k = 0; k < outBlock.length; ++k) { outBlock[k] = tBlock[k].add(d); } } return out; } /** {@inheritDoc} */ @Override public FieldMatrix<T> scalarMultiply(final T d) { final BlockFieldMatrix<T> out = new BlockFieldMatrix<T>(getField(), rows, columns); //

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> perform subtraction block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < out.blocks.length; ++blockIndex) { final T[] outBlock = out.blocks[blockIndex]; final T[] tBlock = blocks[blockIndex]; for (int k = 0; k < outBlock.length; ++k) { outBlock[k] = tBlock[k].multiply(d); } } return out; } /** {@inheritDoc} */ @Override public FieldMatrix<T> multiply(final FieldMatrix<T> m) throws DimensionMismatchException { try { return multiply((BlockFieldMatrix<T>) m); } catch (ClassCastException cce) { // safety check checkMultiplicationCompatible(m); final BlockFieldMatrix<T> out = new BlockFieldMatrix<T>(getField(), rows, m.getColumnDimension()); final T zero = getField().getZero(); // perform multiplication block-wise, to ensure good cache behavior int blockIndex = 0; for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) { final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, m.getColumnDimension()); // select current block final T[] outBlock = out.blocks[blockIndex]; // perform multiplication on current block for (int kBlock = 0; kBlock < blockColumns; ++kBlock) { final int kWidth = blockWidth(kBlock); final T[] tBlock = blocks[iBlock * blockColumns + kBlock]; final int rStart = kBlock * BLOCK_SIZE; int k = 0; for (int p = pStart; p < pEnd; ++p) { final int lStart = (p - pStart) * kWidth; final int lEnd = lStart + kWidth; for (int q = qStart; q < qEnd; ++q) { T sum = zero; int r = rStart; for (int l

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> = lStart; l < lEnd; ++l) { sum = sum.add(tBlock[l].multiply(m.getEntry(r, q))); ++r; } outBlock[k] = outBlock[k].add(sum); ++k; } } } // go to next block ++blockIndex; } } return out; } } /** * Returns the result of postmultiplying {@code this} by {@code m}. * * @param m matrix to postmultiply by * @return {@code this * m} * @throws DimensionMismatchException if the matrices are not compatible. */ public BlockFieldMatrix<T> multiply(BlockFieldMatrix<T> m) throws DimensionMismatchException { // safety check checkMultiplicationCompatible(m); final BlockFieldMatrix<T> out = new BlockFieldMatrix<T>(getField(), rows, m.columns); final T zero = getField().getZero(); // perform multiplication block-wise, to ensure good cache behavior int blockIndex = 0; for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) { final int jWidth = out.blockWidth(jBlock); final int jWidth2 = jWidth + jWidth; final int jWidth3 = jWidth2 + jWidth; final int jWidth4 = jWidth3 + jWidth; // select current block final T[] outBlock = out.blocks[blockIndex]; // perform multiplication on current block for (int kBlock = 0; kBlock < blockColumns; ++kBlock) { final int kWidth = blockWidth(kBlock); final T[] tBlock = blocks[iBlock * blockColumns + kBlock]; final T[] mBlock = m.blocks[kBlock * m.blockColumns + jBlock]; int k = 0; for (int p = pStart; p < pEnd; ++p) { final int lStart = (p - pStart) * kWidth; final int lEnd = lStart + kWidth; for (int n

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>Start = 0; nStart < jWidth; ++nStart) { T sum = zero; int l = lStart; int n = nStart; while (l < lEnd - 3) { sum = sum. add(tBlock[l].multiply(mBlock[n])). add(tBlock[l + 1].multiply(mBlock[n + jWidth])). add(tBlock[l + 2].multiply(mBlock[n + jWidth2])). add(tBlock[l + 3].multiply(mBlock[n + jWidth3])); l += 4; n += jWidth4; } while (l < lEnd) { sum = sum.add(tBlock[l++].multiply(mBlock[n])); n += jWidth; } outBlock[k] = outBlock[k].add(sum); ++k; } } } // go to next block ++blockIndex; } } return out; } /** {@inheritDoc} */ @Override public T[][] getData() { final T[][] data = buildArray(getField(), getRowDimension(), getColumnDimension()); final int lastColumns = columns - (blockColumns - 1) * BLOCK_SIZE; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); int regularPos = 0; int lastPos = 0; for (int p = pStart; p < pEnd; ++p) { final T[] dataP = data[p]; int blockIndex = iBlock * blockColumns; int dataPos = 0; for (int jBlock = 0; jBlock < blockColumns - 1; ++jBlock) { System.arraycopy(blocks[blockIndex++], regularPos, dataP, dataPos, BLOCK_SIZE); dataPos += BLOCK_SIZE; } System.arraycopy(blocks[blockIndex], lastPos, dataP, dataPos, lastColumns); regularPos += BLOCK_SIZE; lastPos += lastColumns; } } return data; } /** {@inheritDoc} */ @Override public FieldMatrix<T> getSubMatrix(

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>_SIZE; // perform copy block-wise, to ensure good cache behavior for (int iBlock = blockStartRow; iBlock < blockEndRow; ++iBlock) { final int iHeight = blockHeight(iBlock); final int firstRow = iBlock * BLOCK_SIZE; final int iStart = FastMath.max(row, firstRow); final int iEnd = FastMath.min(endRow + 1, firstRow + iHeight); for (int jBlock = blockStartColumn; jBlock < blockEndColumn; ++jBlock) { final int jWidth = blockWidth(jBlock); final int firstColumn = jBlock * BLOCK_SIZE; final int jStart = FastMath.max(column, firstColumn); final int jEnd = FastMath.min(endColumn + 1, firstColumn + jWidth); final int jLength = jEnd - jStart; // handle one block, row by row final T[] block = blocks[iBlock * blockColumns + jBlock]; for (int i = iStart; i < iEnd; ++i) { System.arraycopy(subMatrix[i - row], jStart - column, block, (i - firstRow) * jWidth + (jStart - firstColumn), jLength); } } } } /** {@inheritDoc} */ @Override public FieldMatrix<T> getRowMatrix(final int row) throws OutOfRangeException { checkRowIndex(row); final BlockFieldMatrix<T> out = new BlockFieldMatrix<T>(getField(), 1, columns); // perform copy block-wise, to ensure good cache behavior final int iBlock = row / BLOCK_SIZE; final int iRow = row - iBlock * BLOCK_SIZE; int outBlockIndex = 0; int outIndex = 0; T[] outBlock = out.blocks[outBlockIndex]; for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final T[] block = blocks[iBlock * blockColumns + jBlock]; final int available = outBlock.length - outIndex; if (jWidth > available) { System.arraycopy(block, iRow * jWidth, outBlock, outIndex, available);

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> { throw new MatrixDimensionMismatchException(array.length, 1, nRows, 1); } // perform copy block-wise, to ensure good cache behavior final int jBlock = column / BLOCK_SIZE; final int jColumn = column - jBlock * BLOCK_SIZE; final int jWidth = blockWidth(jBlock); int outIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int iHeight = blockHeight(iBlock); final T[] block = blocks[iBlock * blockColumns + jBlock]; for (int i = 0; i < iHeight; ++i) { block[i * jWidth + jColumn] = array[outIndex++]; } } } /** {@inheritDoc} */ @Override public T getEntry(final int row, final int column) throws OutOfRangeException { checkRowIndex(row); checkColumnIndex(column); final int iBlock = row / BLOCK_SIZE; final int jBlock = column / BLOCK_SIZE; final int k = (row - iBlock * BLOCK_SIZE) * blockWidth(jBlock) + (column - jBlock * BLOCK_SIZE); return blocks[iBlock * blockColumns + jBlock][k]; } /** {@inheritDoc} */ @Override public void setEntry(final int row, final int column, final T value) throws OutOfRangeException { checkRowIndex(row); checkColumnIndex(column); final int iBlock = row / BLOCK_SIZE; final int jBlock = column / BLOCK_SIZE; final int k = (row - iBlock * BLOCK_SIZE) * blockWidth(jBlock) + (column - jBlock * BLOCK_SIZE); blocks[iBlock * blockColumns + jBlock][k] = value; } /** {@inheritDoc} */ @Override public void addToEntry(final int row, final int column, final T increment) throws OutOfRangeException { checkRowIndex(row); checkColumnIndex(column); final int iBlock = row / BLOCK_SIZE; final int jBlock = column / BLOCK_SIZE; final int k = (row - iBlock * BLOCK_SIZE) * blockWidth(jBlock) + (column - jBlock * BLOCK_SIZE);

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> final T[] blockIJ = blocks[iBlock * blockColumns + jBlock]; blockIJ[k] = blockIJ[k].add(increment); } /** {@inheritDoc} */ @Override public void multiplyEntry(final int row, final int column, final T factor) throws OutOfRangeException { checkRowIndex(row); checkColumnIndex(column); final int iBlock = row / BLOCK_SIZE; final int jBlock = column / BLOCK_SIZE; final int k = (row - iBlock * BLOCK_SIZE) * blockWidth(jBlock) + (column - jBlock * BLOCK_SIZE); final T[] blockIJ = blocks[iBlock * blockColumns + jBlock]; blockIJ[k] = blockIJ[k].multiply(factor); } /** {@inheritDoc} */ @Override public FieldMatrix<T> transpose() { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); final BlockFieldMatrix<T> out = new BlockFieldMatrix<T>(getField(), nCols, nRows); // perform transpose block-wise, to ensure good cache behavior int blockIndex = 0; for (int iBlock = 0; iBlock < blockColumns; ++iBlock) { for (int jBlock = 0; jBlock < blockRows; ++jBlock) { // transpose current block final T[] outBlock = out.blocks[blockIndex]; final T[] tBlock = blocks[jBlock * blockColumns + iBlock]; final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, columns); final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, rows); int k = 0; for (int p = pStart; p < pEnd; ++p) { final int lInc = pEnd - pStart; int l = p - pStart; for (int q = qStart; q < qEnd; ++q) { outBlock[k] = tBlock[l]; ++k; l+= lInc; } } // go to next block ++blockIndex; } } return out; } /** {@inheritDoc} */

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> @Override public int getRowDimension() { return rows; } /** {@inheritDoc} */ @Override public int getColumnDimension() { return columns; } /** {@inheritDoc} */ @Override public T[] operate(final T[] v) throws DimensionMismatchException { if (v.length != columns) { throw new DimensionMismatchException(v.length, columns); } final T[] out = buildArray(getField(), rows); final T zero = getField().getZero(); // perform multiplication block-wise, to ensure good cache behavior for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final T[] block = blocks[iBlock * blockColumns + jBlock]; final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); int k = 0; for (int p = pStart; p < pEnd; ++p) { T sum = zero; int q = qStart; while (q < qEnd - 3) { sum = sum. add(block[k].multiply(v[q])). add(block[k + 1].multiply(v[q + 1])). add(block[k + 2].multiply(v[q + 2])). add(block[k + 3].multiply(v[q + 3])); k += 4; q += 4; } while (q < qEnd) { sum = sum.add(block[k++].multiply(v[q++])); } out[p] = out[p].add(sum); } } } return out; } /** {@inheritDoc} */ @Override public T[] preMultiply(final T[] v) throws DimensionMismatchException { if (v.length != rows) { throw new DimensionMismatchException(v.length, rows); } final T[] out = buildArray(getField(), columns); final T zero = getField().getZero(); // perform multiplication block-wise,

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> to ensure good cache behavior for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final int jWidth2 = jWidth + jWidth; final int jWidth3 = jWidth2 + jWidth; final int jWidth4 = jWidth3 + jWidth; final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final T[] block = blocks[iBlock * blockColumns + jBlock]; final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int q = qStart; q < qEnd; ++q) { int k = q - qStart; T sum = zero; int p = pStart; while (p < pEnd - 3) { sum = sum. add(block[k].multiply(v[p])). add(block[k + jWidth].multiply(v[p + 1])). add(block[k + jWidth2].multiply(v[p + 2])). add(block[k + jWidth3].multiply(v[p + 3])); k += jWidth4; p += 4; } while (p < pEnd) { sum = sum.add(block[k].multiply(v[p++])); k += jWidth; } out[q] = out[q].add(sum); } } } return out; } /** {@inheritDoc} */ @Override public T walkInRowOrder(final FieldMatrixChangingVisitor<T> visitor) { visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int p = pStart; p < pEnd; ++p) { for (int jBlock = 0; jBlock < block

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>Columns; ++jBlock) { final int jWidth = blockWidth(jBlock); final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final T[] block = blocks[iBlock * blockColumns + jBlock]; int k = (p - pStart) * jWidth; for (int q = qStart; q < qEnd; ++q) { block[k] = visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); } /** {@inheritDoc} */ @Override public T walkInRowOrder(final FieldMatrixPreservingVisitor<T> visitor) { visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int p = pStart; p < pEnd; ++p) { for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final T[] block = blocks[iBlock * blockColumns + jBlock]; int k = (p - pStart) * jWidth; for (int q = qStart; q < qEnd; ++q) { visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); } /** {@inheritDoc} */ @Override public T walkInRowOrder(final FieldMatrixChangingVisitor<T> visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); visitor.start(rows, columns, startRow, endRow, startColumn, endColumn); for (int iBlock = startRow / BLOCK_SIZE; iBlock <

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>1 + endRow / BLOCK_SIZE; ++iBlock) { final int p0 = iBlock * BLOCK_SIZE; final int pStart = FastMath.max(startRow, p0); final int pEnd = FastMath.min((iBlock + 1) * BLOCK_SIZE, 1 + endRow); for (int p = pStart; p < pEnd; ++p) { for (int jBlock = startColumn / BLOCK_SIZE; jBlock < 1 + endColumn / BLOCK_SIZE; ++jBlock) { final int jWidth = blockWidth(jBlock); final int q0 = jBlock * BLOCK_SIZE; final int qStart = FastMath.max(startColumn, q0); final int qEnd = FastMath.min((jBlock + 1) * BLOCK_SIZE, 1 + endColumn); final T[] block = blocks[iBlock * blockColumns + jBlock]; int k = (p - p0) * jWidth + qStart - q0; for (int q = qStart; q < qEnd; ++q) { block[k] = visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); } /** {@inheritDoc} */ @Override public T walkInRowOrder(final FieldMatrixPreservingVisitor<T> visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); visitor.start(rows, columns, startRow, endRow, startColumn, endColumn); for (int iBlock = startRow / BLOCK_SIZE; iBlock < 1 + endRow / BLOCK_SIZE; ++iBlock) { final int p0 = iBlock * BLOCK_SIZE; final int pStart = FastMath.max(startRow, p0); final int pEnd = FastMath.min((iBlock + 1) * BLOCK_SIZE, 1 + endRow); for (int p = pStart; p < pEnd; ++p) { for (int jBlock = startColumn / BLOCK_SIZE; jBlock < 1 + endColumn / BLOCK_SIZE; ++jBlock) { final

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS> int jWidth = blockWidth(jBlock); final int q0 = jBlock * BLOCK_SIZE; final int qStart = FastMath.max(startColumn, q0); final int qEnd = FastMath.min((jBlock + 1) * BLOCK_SIZE, 1 + endColumn); final T[] block = blocks[iBlock * blockColumns + jBlock]; int k = (p - p0) * jWidth + qStart - q0; for (int q = qStart; q < qEnd; ++q) { visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); } /** {@inheritDoc} */ @Override public T walkInOptimizedOrder(final FieldMatrixChangingVisitor<T> visitor) { visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); int blockIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final T[] block = blocks[blockIndex]; int k = 0; for (int p = pStart; p < pEnd; ++p) { for (int q = qStart; q < qEnd; ++q) { block[k] = visitor.visit(p, q, block[k]); ++k; } } ++blockIndex; } } return visitor.end(); } /** {@inheritDoc} */ @Override public T walkInOptimizedOrder(final FieldMatrixPreservingVisitor<T> visitor) { visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); int blockIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>(pStart + BLOCK_SIZE, rows); for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final T[] block = blocks[blockIndex]; int k = 0; for (int p = pStart; p < pEnd; ++p) { for (int q = qStart; q < qEnd; ++q) { visitor.visit(p, q, block[k]); ++k; } } ++blockIndex; } } return visitor.end(); } /** {@inheritDoc} */ @Override public T walkInOptimizedOrder(final FieldMatrixChangingVisitor<T> visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); visitor.start(rows, columns, startRow, endRow, startColumn, endColumn); for (int iBlock = startRow / BLOCK_SIZE; iBlock < 1 + endRow / BLOCK_SIZE; ++iBlock) { final int p0 = iBlock * BLOCK_SIZE; final int pStart = FastMath.max(startRow, p0); final int pEnd = FastMath.min((iBlock + 1) * BLOCK_SIZE, 1 + endRow); for (int jBlock = startColumn / BLOCK_SIZE; jBlock < 1 + endColumn / BLOCK_SIZE; ++jBlock) { final int jWidth = blockWidth(jBlock); final int q0 = jBlock * BLOCK_SIZE; final int qStart = FastMath.max(startColumn, q0); final int qEnd = FastMath.min((jBlock + 1) * BLOCK_SIZE, 1 + endColumn); final T[] block = blocks[iBlock * blockColumns + jBlock]; for (int p = pStart; p < pEnd; ++p) { int k = (p - p0) * jWidth + qStart - q0; for (int q = qStart; q < qEnd; ++q) { block[k] = visitor

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>.visit(p, q, block[k]); ++k; } } } } return visitor.end(); } /** {@inheritDoc} */ @Override public T walkInOptimizedOrder(final FieldMatrixPreservingVisitor<T> visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); visitor.start(rows, columns, startRow, endRow, startColumn, endColumn); for (int iBlock = startRow / BLOCK_SIZE; iBlock < 1 + endRow / BLOCK_SIZE; ++iBlock) { final int p0 = iBlock * BLOCK_SIZE; final int pStart = FastMath.max(startRow, p0); final int pEnd = FastMath.min((iBlock + 1) * BLOCK_SIZE, 1 + endRow); for (int jBlock = startColumn / BLOCK_SIZE; jBlock < 1 + endColumn / BLOCK_SIZE; ++jBlock) { final int jWidth = blockWidth(jBlock); final int q0 = jBlock * BLOCK_SIZE; final int qStart = FastMath.max(startColumn, q0); final int qEnd = FastMath.min((jBlock + 1) * BLOCK_SIZE, 1 + endColumn); final T[] block = blocks[iBlock * blockColumns + jBlock]; for (int p = pStart; p < pEnd; ++p) { int k = (p - p0) * jWidth + qStart - q0; for (int q = qStart; q < qEnd; ++q) { visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); } /** * Get the height of a block. * @param blockRow row index (in block sense) of the block * @return height (number of rows) of the block */ private int blockHeight(final int blockRow) { return (blockRow == blockRows - 1) ? rows - blockRow * BLOCK_SIZE : BLOCK_SIZE; } /** * Get the width of a block. * @param blockColumn

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>, MatrixDimensionMismatchException; /** * Get the entry in the specified row and column. Row and column indices * start at 0. * * @param row Row index of entry to be fetched. * @param column Column index of entry to be fetched. * @return the matrix entry at {@code (row, column)}. * @throws OutOfRangeException if the row or column index is not valid. */ double getEntry(int row, int column) throws OutOfRangeException; /** * Set the entry in the specified row and column. Row and column indices * start at 0. * * @param row Row index of entry to be set. * @param column Column index of entry to be set. * @param value the new value of the entry. * @throws OutOfRangeException if the row or column index is not valid * @since 2.0 */ void setEntry(int row, int column, double value) throws OutOfRangeException; /** * Adds (in place) the specified value to the specified entry of * {@code this} matrix. Row and column indices start at 0. * * @param row Row index of the entry to be modified. * @param column Column index of the entry to be modified. * @param increment value to add to the matrix entry. * @throws OutOfRangeException if the row or column index is not valid. * @since 2.0 */ void addToEntry(int row, int column, double increment) throws OutOfRangeException; /** * Multiplies (in place) the specified entry of {@code this} matrix by the * specified value. Row and column indices start at 0. * * @param row Row index of the entry to be modified. * @param column Column index of the entry to be modified. * @param factor Multiplication factor for the matrix entry. * @throws OutOfRangeException if the row or column index is not valid. * @since 2.0 */ void multiplyEntry(int row, int column, double factor) throws OutOfRangeException; /** * Returns the transpose of this matrix. * * @return transpose matrix */ RealMatrix transpose(); /** * Returns the <a href="http://mathworld.wolfram.com/MatrixTrace.html"> * trace</a> of the matrix

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>DimensionMismatchException if the vector dimension does not * match one instance column. */ void setColumnVector(int column, FieldVector<T> vector) throws MatrixDimensionMismatchException, OutOfRangeException; /** * Get the entries in row number {@code row} as an array. * * @param row Row to be fetched. * @return array of entries in the row. * @throws OutOfRangeException if the specified row index is not valid. */ T[] getRow(int row) throws OutOfRangeException; /** * Set the entries in row number {@code row} * as a row matrix. * * @param row Row to be set. * @param array Row matrix (must have the same number of columns as * the instance). * @throws OutOfRangeException if the specified row index is invalid. * @throws MatrixDimensionMismatchException if the array size does not match * one instance row. */ void setRow(int row, T[] array) throws MatrixDimensionMismatchException, OutOfRangeException; /** * Get the entries in column number {@code col} as an array. * * @param column the column to be fetched * @return array of entries in the column * @throws OutOfRangeException if the specified column index is not valid. */ T[] getColumn(int column) throws OutOfRangeException; /** * Set the entries in column number {@code column} * as a column matrix. * * @param column the column to be set * @param array column array (must have the same number of rows as the instance) * @throws OutOfRangeException if the specified column index is invalid. * @throws MatrixDimensionMismatchException if the array size does not match * one instance column. */ void setColumn(int column, T[] array) throws MatrixDimensionMismatchException, OutOfRangeException; /** * Returns the entry in the specified row and column. * * @param row row location of entry to be fetched * @param column column location of entry to be fetched * @return matrix entry in row,column * @throws OutOfRangeException if the row or column index is not valid. */ T getEntry(int row, int column) throws OutOfRangeException; /** * Set the entry in the specified row and column. * * @

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>ROWS_NOT_INITIALIZED_YET, row); } if (column > 0) { throw new MathIllegalStateException(LocalizedFormats.FIRST_COLUMNS_NOT_INITIALIZED_YET, column); } final int nRows = subMatrix.length; if (nRows == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); } final int nCols = subMatrix[0].length; if (nCols == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN); } data = buildArray(getField(), subMatrix.length, nCols); for (int i = 0; i < data.length; ++i) { if (subMatrix[i].length != nCols) { throw new DimensionMismatchException(nCols, subMatrix[i].length); } System.arraycopy(subMatrix[i], 0, data[i + row], column, nCols); } } else { super.setSubMatrix(subMatrix, row, column); } } /** {@inheritDoc} */ @Override public T getEntry(final int row, final int column) throws OutOfRangeException { checkRowIndex(row); checkColumnIndex(column); return data[row][column]; } /** {@inheritDoc} */ @Override public void setEntry(final int row, final int column, final T value) throws OutOfRangeException { checkRowIndex(row); checkColumnIndex(column); data[row][column] = value; } /** {@inheritDoc} */ @Override public void addToEntry(final int row, final int column, final T increment) throws OutOfRangeException { checkRowIndex(row); checkColumnIndex(column); data[row][column] = data[row][column].add(increment); } /** {@inheritDoc} */ @Override public void multiplyEntry(final int row, final int column, final T factor) throws OutOfRangeException { checkRowIndex(row); checkColumnIndex(column); data[row][column] = data[row][column].multiply(factor); } /** {@inheritDoc} */ @Override public int getRowDimension() { return (data == null) ? 0 : data.length; } /** {@inheritDoc} */

Math, 14

<FILEB>
<CHANGES>
weightMatrix = new DiagonalMatrix(weight);
<CHANGEE>
<FILEE>
<FILEB>
<CHANGES>
if (m instanceof DiagonalMatrix) {
final int dim = m.getRowDimension();
final RealMatrix sqrtM = new DiagonalMatrix(dim);
for (int i = 0; i < dim; i++) {
sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
}
return sqrtM;
} else {
<CHANGEE>
<CHANGES>
}
<CHANGEE>
<FILEE>
<FILEB> /** * Weight matrix of the residuals between model and observations. * <br/> * Immutable class. * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { final int dim = weight.length; <CHANGES> weightMatrix = org.apache.commons.math3.linear.MatrixUtils.createRealMatrix(dim, dim); for (int i = 0; i < dim; i++) { weightMatrix.setEntry(i, i, weight[i]); } <CHANGEE> } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); <FILEE> <FILEB> // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { <CHANGES> <CHANGEE> final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); <CHANGES> <CHANGEE> } } <FILEE> <SCANS>-12-03 19:37:14Z tn $ * @since 2.0 * */ public class GaussNewtonOptimizer extends AbstractLeastSquaresOptimizer { /** Indicator for using LU decomposition. */ private final boolean useLU; /** * Simple constructor with default settings. * The normal equations will be solved using LU decomposition. * * @param checker Convergence checker. */ public GaussNewtonOptimizer(ConvergenceChecker<PointVectorValuePair> checker) { this(true, checker); } /** * @param useLU If {@code true}, the normal equations will be solved * using LU decomposition, otherwise they will be solved using QR * decomposition. * @param checker Convergence checker. */ public GaussNewtonOptimizer(final boolean useLU, ConvergenceChecker<PointVectorValuePair> checker) { super(checker); this.useLU = useLU; } /** {@inheritDoc} */ @Override public PointVectorValuePair doOptimize() { final ConvergenceChecker<PointVectorValuePair> checker = getConvergenceChecker(); // Computation will be useless without a checker (see "for-loop"). if (checker == null) { throw new NullArgumentException(); } final double[] targetValues = getTarget(); final int nR = targetValues.length; // Number of observed data. final RealMatrix weightMatrix = getWeight(); // Diagonal of the weight matrix. final double[] residualsWeights = new double[nR]; for (int i = 0; i < nR; i++) { residualsWeights[i] = weightMatrix.getEntry(i, i); } final double[] currentPoint = getStartPoint(); final int nC = currentPoint.length; // iterate until convergence is reached PointVectorValuePair current = null; int iter = 0; for (boolean converged = false; !converged;) { ++iter; // evaluate the objective function and its jacobian PointVectorValuePair previous = current; // Value of the objective function at "currentPoint". final double[] currentObjective = computeObjectiveValue(currentPoint); final double[] currentResiduals = computeResiduals(currentObjective); final RealMatrix weightedJacobian = computeWeightedJacobian(currentPoint); current = new PointVectorValuePair(currentPoint, currentObjective